DOC PREVIEW
Penn CIT 591 - Polymorphism

This preview shows page 1-2-3-4 out of 13 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 13 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 13 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 13 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 13 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 13 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

PolymorphismLegal assignmentsLegal method callsIllegal method callsOverloadingWhy overload a method?Slide 7SignaturesShadowingOverridingHow to override a methodWhy override a method?The EndPolymorphismLegal 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 }}Legal 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.0Illegal 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)Overloadingclass 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) { System.out.println("double d = " + d); }}int i = 5double d = 5.0Why overload a method?•Sometimes so you can supply defaults for the parameters int increment() { return increment(1);}•Sometimes so you can supply additional information: int printResult(String message) { System.out.println(message); printResult();}Polymorphism•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 (which you have just seen)•Two or more methods with different signatures–Overriding (which you will see shortly)•Replacing an inherited method with another having the same signatureSignatures•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!Shadowing•This is called shadowing—name in class Dog shadows name in class Animalclass Animal { String name = "Animal"; public static void main(String args[]) { Animal animal = new Animal(); Dog dog = new Dog(); System.out.println(animal.name + " " + dog.name); }}public class Dog extends Animal { String name = "Dog";}Animal DogOverriding•This is called overriding a method•Method print in Dog overrides method print in Animalclass Animal { public static void main(String args[]) { Animal animal = new Animal(); Dog dog = new Dog(); animal.print(); dog.print(); } static void print() { System.out.println("Superclass Animal"); }}public class Dog extends Animal { static void print() { System.out.println("Subclass Dog"); }}Superclass AnimalSubclass DogHow to override a method•Create a method in a subclass having the same name and the same number and types of parameters–Parameter names don’t matter–The return type must be the same•The overriding method cannot be more private than the method it overridesWhy override a method?•Dog dog = new dog();System.out.println(dog);–Prints something like Dog@feda4c00•Add to class Dog the following: public String toString() { return name;} Now System.out.println(dog); prints something like: FidoThe


View Full Document

Penn CIT 591 - Polymorphism

Documents in this Course
Stacks

Stacks

11 pages

Arrays

Arrays

30 pages

Arrays

Arrays

29 pages

Applets

Applets

24 pages

Style

Style

33 pages

JUnit

JUnit

23 pages

Java

Java

32 pages

Access

Access

18 pages

Methods

Methods

29 pages

Arrays

Arrays

32 pages

Methods

Methods

9 pages

Methods

Methods

29 pages

Vectors

Vectors

14 pages

Eclipse

Eclipse

23 pages

Vectors

Vectors

14 pages

Recursion

Recursion

24 pages

Animation

Animation

18 pages

Animation

Animation

18 pages

Static

Static

12 pages

Eclipse

Eclipse

23 pages

JAVA

JAVA

24 pages

Arrays

Arrays

29 pages

Animation

Animation

18 pages

Numbers

Numbers

21 pages

JUnit

JUnit

23 pages

Access

Access

18 pages

Applets

Applets

24 pages

Methods

Methods

30 pages

Buttons

Buttons

20 pages

Java

Java

31 pages

Style

Style

28 pages

Style

Style

28 pages

Load more
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?