Presentation is loading. Please wait.

Presentation is loading. Please wait.

Polymorphism & Methods COMP204 Bernhard Pfahringer (with lots of input from Mark Hall) poly + morphos (greek) “many forms”

Similar presentations


Presentation on theme: "Polymorphism & Methods COMP204 Bernhard Pfahringer (with lots of input from Mark Hall) poly + morphos (greek) “many forms”"— Presentation transcript:

1 Polymorphism & Methods COMP204 Bernhard Pfahringer (with lots of input from Mark Hall) poly + morphos (greek) “many forms”

2 Overriding Subclass redefines a method: –different behaviour –or maybe modified behaviour class Rectangle { public String toString() { return “ ”; }

3 Inheritance Subclasses inherit everything from superclasses, but cannot directly access private fields or methods, only public and protected ones. Can still access overridden methods: public void roam() { super.roam(); // some additional local stuff }

4 Inheritance: Is-A (not Has-A) B should extend A if “B is-A A” –Triangle Is-A Shape –Surgeon Is-A Medical Doctor Bathroom Has-A Tub –Tub is an instance variable of Bathroom (Composition)

5 Code Inheritance Use inheritance to implement shared behaviour (code) only once! Always avoid code duplication! –easier to change (only one place), especially when debugging

6 Polymorphism what happens when one declares and initializes a reference variable: Dog myDog = new Dog(); allocate space for reference variable of type Dog allocate space for a new Dog object on the heap (and initialize it) point the reference to the new object

7 Polymorphism Reference type can be any super class or Interface implemented by actual type: Animal myDog = new Dog(); As generic as possible => more flexible code: Map myMap = new HashMap ();

8 Polymorphism Animal[] animals = new Animal[]{new Dog(), new Lion(), new Cat(), new Wolf(), new Hippo()}; for(Animal a: animals) { a.eat(); a.roam(); }

9 Polymorphism Parameters/Arguments and return values can be polymorphic too: class Vet { public void giveShot(Animal a) { a.makeNoise(); } class PetOwner { public void start() { Vet v = new Vet(); Dog d = new Dog(); Hippo h = new Hippo(); v.giveShot(d); v.giveShot(h); }

10 stop overriding final declaration: –class: cannot extend/subclass any further final public class String {.. } –method: cannot be overridden in subclasses class Cat { final public void makeNoise() {…} –field: cannot change value final int count = animals.length;

11 Overriding rules Argument and return type must be the same: class Appliance { public boolean turnOn(); } class Toaster extends Appliance { public boolean turnOn(int level); // OVERLOADING }

12 Overriding rules May NOT be less accessible: class Appliance { public boolean turnOn(); } class Toaster extends Appliance { protected boolean turnOn(); // illegal }

13 Overloading Two (or more) methods with the same name but different argument lists (see code example) Usually best avoided, can be very confusing and counter-intuitive

14 Which method is executed Compile time: compiler ensures that method with appropriate signature (compile-time type info for arguments) exists in “compile-time” receiver class Runtime: given the actual runtime-type of the receiver, the most specific method of appropriate signature is located and executed ==> runtime types of arguments do NOT matter (again, see code examples)


Download ppt "Polymorphism & Methods COMP204 Bernhard Pfahringer (with lots of input from Mark Hall) poly + morphos (greek) “many forms”"

Similar presentations


Ads by Google