Unformatted text preview:

PolymorphismSignaturesSlide 3OverloadingWhy overload a method?DRY (Don’t Repeat Yourself)Another reason to overload methodsLegal assignmentsLegal method callsIllegal method callsJava uses the most specific methodMultiple constructors IMultiple constructors IISuperclass construction ISuperclass construction IIShadowingAn aside: NamespacesOverridingHow to override a methodWhy override a method?More about toString()EqualityThe equals methodCalling an overridden methodSummaryThe EndJan 14, 2019Polymorphism2SignaturesIn any programming language, a signature is what distinguishes one function or method from anotherIn C, every function has to have a different nameIn Java, two methods have to differ in their names or in the number or types of their parametersfoo(int i) and foo(int i, int j) are differentfoo(int i) and foo(int k) are the samefoo(int i, double d) and foo(double d, int i) are differentIn C++, the signature also includes the return typeBut not in Java!3PolymorphismPolymorphism means many (poly) shapes (morph)In Java, polymorphism refers to the fact that you can have multiple methods with the same name in the same classThere are two kinds of polymorphism:OverloadingTwo or more methods with different signaturesOverridingReplacing an inherited method with another having the same signature4Overloadingclass Test { public static void main(String args[]) { myPrint(5); myPrint(5.0); } static void myPrint(int i) { System.out.println("int i = " + i); } static void myPrint(double d) { // same name, different parameters System.out.println("double d = " + d); }}int i = 5double d = 5.05Why overload a method?So you can use the same names for methods that do essentially the same thingExample: println(int), println(double), println(boolean), println(String), etc.So you can supply defaults for the parameters: int increment(int amount) { count = count + amount; return count;} int increment() { return increment(1);}Notice that one method can call another of the same nameSo you can supply additional information: void printResults() { System.out.println("total = " + total + ", average = " + average);} void printResult(String message) { System.out.println(message + ": "); printResults();}6DRY (Don’t Repeat Yourself)When you overload a method with another, very similar method, only one of them should do most of the work: void debug() { System.out.println("first = " + first + ", last = " + last); for (int i = first; i <= last; i++) { System.out.print(dictionary[i] + " "); } System.out.println();} void debug(String s) { System.out.println("At checkpoint " + s + ":"); debug();}7Another reason to overload methodsYou may want to do “the same thing” with different kinds of data:class Student extends Person { ... void printInformation() { printPersonalInformation(); printGrades(); }}class Professor extends Person() { ... void printInformation() { printPersonalInformation(); printResearchInterests(); }}Java’s print and println methods are heavily overloaded8Legal assignmentsWidening is legalNarrowing is illegal (unless you cast)class Test { public static void main(String args[]) { double d; int i; d = 5; // legal i = 3.5; // illegal i = (int) 3.5; // legal }}9Legal method callsLegal because parameter transmission is equivalent to assignmentmyPrint(5) is like double d = 5; System.out.println(d);class Test { public static void main(String args[]) { myPrint(5); } static void myPrint(double d) { System.out.println(d); }}5.010Illegal method callsIllegal because parameter transmission is equivalent to assignmentmyPrint(5.0) is like int i = 5.0; System.out.println(i);class Test { public static void main(String args[]) { myPrint(5.0); } static void myPrint(int i) { System.out.println(i); }}myPrint(int) in Test cannot be applied to (double)11Java uses the most specific methodclass Test { public static void main(String args[]) { myPrint(5); myPrint(5.0); } static void myPrint(double d) { System.out.println("double: " + d); } static void myPrint(int i) { System.out.println("int: " + i); }}int:5double: 5.012Multiple constructors IYou can “overload” constructors as well as methods:Counter() { count = 0;}Counter(int start) { count = start;}13Multiple constructors IIOne constructor can “call” another constructor in the same class, but there are special rulesYou call the other constructor with the keyword thisThe call must be the very first thing the constructor doesPoint(int x, int y) { this.x = x; this.y = y; sum = x + y;}Point() { this(0, 0);}A common reason for overloading constructors is (as above) to provide default values for missing parameters14Superclass construction IThe very first thing any constructor does, automatically, is call the default constructor for its superclassclass Foo extends Bar { Foo() { // constructor super(); // invisible call to superclass constructor ...You can replace this with a call to a specific superclass constructorUse the keyword superThis must be the very first thing the constructor doesclass Foo extends Bar { Foo(String name) { // constructor super(name, 5); // explicit call to superclass constructor ...15Superclass construction IIUnless you specify otherwise, every constructor calls the default constructor for its superclassclass Foo extends Bar { Foo() { // constructor super(); // invi sible call to superclass constructor ...You can use this(...) to call another constructor in the same class:class Foo extends Bar { Foo(String message) { // constructor this(message, 0, 0); // your explici t call to another constructor ...You can use super(...) to call a specific superclass constructorclass Foo extends Bar { Foo(String name) { // constructor super(name, 5); // your explicit call to some superclass constructor ...Since the call to another constructor must be the very first thing you do in the constructor, you can only do one of the above16ShadowingThis is called shadowing—name in class Dog shadows name in class Animalclass Animal { String name = "Animal"; public static void


View Full Document

Penn CIT 590 - Polymorphism

Download Polymorphism
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Polymorphism and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Polymorphism 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?