Polymorphism & Methods COMP206 Geoff Holmes & Bernhard Pfahringer (these slides: lots of input from...
-
Upload
cory-matthews -
Category
Documents
-
view
217 -
download
0
Transcript of Polymorphism & Methods COMP206 Geoff Holmes & Bernhard Pfahringer (these slides: lots of input from...
![Page 1: Polymorphism & Methods COMP206 Geoff Holmes & Bernhard Pfahringer (these slides: lots of input from Mark Hall) poly + morphos (greek) “many forms”](https://reader035.fdocuments.net/reader035/viewer/2022062519/5697c00d1a28abf838cc981a/html5/thumbnails/1.jpg)
Polymorphism & Methods
COMP206 Geoff Holmes & Bernhard Pfahringer
(these slides: lots of input from Mark Hall)
poly + morphos (greek) “many forms”
![Page 2: Polymorphism & Methods COMP206 Geoff Holmes & Bernhard Pfahringer (these slides: lots of input from Mark Hall) poly + morphos (greek) “many forms”](https://reader035.fdocuments.net/reader035/viewer/2022062519/5697c00d1a28abf838cc981a/html5/thumbnails/2.jpg)
Overriding
• Subclass redefines a method:– different behaviour– or maybe modified behaviour
class Rectangle {public String toString() {
return “<Rectangle @ ”+x+”/”+y+” “+width+” by “+height+”>”;
}
![Page 3: Polymorphism & Methods COMP206 Geoff Holmes & Bernhard Pfahringer (these slides: lots of input from Mark Hall) poly + morphos (greek) “many forms”](https://reader035.fdocuments.net/reader035/viewer/2022062519/5697c00d1a28abf838cc981a/html5/thumbnails/3.jpg)
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}
![Page 4: Polymorphism & Methods COMP206 Geoff Holmes & Bernhard Pfahringer (these slides: lots of input from Mark Hall) poly + morphos (greek) “many forms”](https://reader035.fdocuments.net/reader035/viewer/2022062519/5697c00d1a28abf838cc981a/html5/thumbnails/4.jpg)
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)
![Page 5: Polymorphism & Methods COMP206 Geoff Holmes & Bernhard Pfahringer (these slides: lots of input from Mark Hall) poly + morphos (greek) “many forms”](https://reader035.fdocuments.net/reader035/viewer/2022062519/5697c00d1a28abf838cc981a/html5/thumbnails/5.jpg)
Code Inheritance
• Use inheritance to implement shared behaviour (code) only once!
• Always avoid code duplication!– easier to change (only one place),
especially when debugging
![Page 6: Polymorphism & Methods COMP206 Geoff Holmes & Bernhard Pfahringer (these slides: lots of input from Mark Hall) poly + morphos (greek) “many forms”](https://reader035.fdocuments.net/reader035/viewer/2022062519/5697c00d1a28abf838cc981a/html5/thumbnails/6.jpg)
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
![Page 7: Polymorphism & Methods COMP206 Geoff Holmes & Bernhard Pfahringer (these slides: lots of input from Mark Hall) poly + morphos (greek) “many forms”](https://reader035.fdocuments.net/reader035/viewer/2022062519/5697c00d1a28abf838cc981a/html5/thumbnails/7.jpg)
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<String,Object> myMap = new HashMap<String,Object>();
![Page 8: Polymorphism & Methods COMP206 Geoff Holmes & Bernhard Pfahringer (these slides: lots of input from Mark Hall) poly + morphos (greek) “many forms”](https://reader035.fdocuments.net/reader035/viewer/2022062519/5697c00d1a28abf838cc981a/html5/thumbnails/8.jpg)
Polymorphism
Animal[] animals = new Animal[]{new Dog(), new Lion(), new Cat(), new Wolf(), new Hippo()};
for(Animal a: animals) {a.eat();
a.roam();}
![Page 9: Polymorphism & Methods COMP206 Geoff Holmes & Bernhard Pfahringer (these slides: lots of input from Mark Hall) poly + morphos (greek) “many forms”](https://reader035.fdocuments.net/reader035/viewer/2022062519/5697c00d1a28abf838cc981a/html5/thumbnails/9.jpg)
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); }}
![Page 10: Polymorphism & Methods COMP206 Geoff Holmes & Bernhard Pfahringer (these slides: lots of input from Mark Hall) poly + morphos (greek) “many forms”](https://reader035.fdocuments.net/reader035/viewer/2022062519/5697c00d1a28abf838cc981a/html5/thumbnails/10.jpg)
How to 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;
![Page 11: Polymorphism & Methods COMP206 Geoff Holmes & Bernhard Pfahringer (these slides: lots of input from Mark Hall) poly + morphos (greek) “many forms”](https://reader035.fdocuments.net/reader035/viewer/2022062519/5697c00d1a28abf838cc981a/html5/thumbnails/11.jpg)
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
}
![Page 12: Polymorphism & Methods COMP206 Geoff Holmes & Bernhard Pfahringer (these slides: lots of input from Mark Hall) poly + morphos (greek) “many forms”](https://reader035.fdocuments.net/reader035/viewer/2022062519/5697c00d1a28abf838cc981a/html5/thumbnails/12.jpg)
Overriding rules
• May NOT be less accessible:
class Appliance {
public boolean turnOn();
}
class Toaster extends Appliance {
protected boolean turnOn();
// illegal
}
![Page 13: Polymorphism & Methods COMP206 Geoff Holmes & Bernhard Pfahringer (these slides: lots of input from Mark Hall) poly + morphos (greek) “many forms”](https://reader035.fdocuments.net/reader035/viewer/2022062519/5697c00d1a28abf838cc981a/html5/thumbnails/13.jpg)
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
![Page 14: Polymorphism & Methods COMP206 Geoff Holmes & Bernhard Pfahringer (these slides: lots of input from Mark Hall) poly + morphos (greek) “many forms”](https://reader035.fdocuments.net/reader035/viewer/2022062519/5697c00d1a28abf838cc981a/html5/thumbnails/14.jpg)
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)
![Page 15: Polymorphism & Methods COMP206 Geoff Holmes & Bernhard Pfahringer (these slides: lots of input from Mark Hall) poly + morphos (greek) “many forms”](https://reader035.fdocuments.net/reader035/viewer/2022062519/5697c00d1a28abf838cc981a/html5/thumbnails/15.jpg)
Java Generics
(with input from Robi Malik)
![Page 16: Polymorphism & Methods COMP206 Geoff Holmes & Bernhard Pfahringer (these slides: lots of input from Mark Hall) poly + morphos (greek) “many forms”](https://reader035.fdocuments.net/reader035/viewer/2022062519/5697c00d1a28abf838cc981a/html5/thumbnails/16.jpg)
Generic types for collections
• Old Java (1.4 and older):List strings = new ArrayList();
strings.add(“hello”);
String word = (String) strings.get(0);
• New (since 1.5):
List<String> strings = new ArrayList<String>();
strings.add(“hello”);
String word = strings.get(0);
![Page 17: Polymorphism & Methods COMP206 Geoff Holmes & Bernhard Pfahringer (these slides: lots of input from Mark Hall) poly + morphos (greek) “many forms”](https://reader035.fdocuments.net/reader035/viewer/2022062519/5697c00d1a28abf838cc981a/html5/thumbnails/17.jpg)
Advantages
• Better readability
• Better type-safety: no casts (runtime checks), compiler can already catch problems
![Page 18: Polymorphism & Methods COMP206 Geoff Holmes & Bernhard Pfahringer (these slides: lots of input from Mark Hall) poly + morphos (greek) “many forms”](https://reader035.fdocuments.net/reader035/viewer/2022062519/5697c00d1a28abf838cc981a/html5/thumbnails/18.jpg)
Writing your own generic code
public class Stack<E> { public void push(E element) { contents.add(element); } public E pop() { int top = contents.size()-1; E result = contents.get(top); contents.remove(top); return result; } private List<E> contents = new ArrayList<E>();}
![Page 19: Polymorphism & Methods COMP206 Geoff Holmes & Bernhard Pfahringer (these slides: lots of input from Mark Hall) poly + morphos (greek) “many forms”](https://reader035.fdocuments.net/reader035/viewer/2022062519/5697c00d1a28abf838cc981a/html5/thumbnails/19.jpg)
Formal type parameter
public class Stack<E> { … }
– convention: Short (single-char) uppercase– can be used wherever a Type is needed– will be replaced with actual Type
![Page 20: Polymorphism & Methods COMP206 Geoff Holmes & Bernhard Pfahringer (these slides: lots of input from Mark Hall) poly + morphos (greek) “many forms”](https://reader035.fdocuments.net/reader035/viewer/2022062519/5697c00d1a28abf838cc981a/html5/thumbnails/20.jpg)
Problems with sub-types
class Student extends Person { .. }
List<Student> students = new ArrayList<Student>();
List<Person> people = students;
// should this be possible? -> no
![Page 21: Polymorphism & Methods COMP206 Geoff Holmes & Bernhard Pfahringer (these slides: lots of input from Mark Hall) poly + morphos (greek) “many forms”](https://reader035.fdocuments.net/reader035/viewer/2022062519/5697c00d1a28abf838cc981a/html5/thumbnails/21.jpg)
No, because
Person sam = new Person();
people.add(sam);
// if this was legal, we would have just sneaked a non-student onto the students list
on the other hand, how do you write generic code then accepting lists of sub-types of Persons ???
![Page 22: Polymorphism & Methods COMP206 Geoff Holmes & Bernhard Pfahringer (these slides: lots of input from Mark Hall) poly + morphos (greek) “many forms”](https://reader035.fdocuments.net/reader035/viewer/2022062519/5697c00d1a28abf838cc981a/html5/thumbnails/22.jpg)
Wildcards
void printCollection(Collection<?> c) {
for(Object o: c) {
System.out.println(o);
}
}
// only read access (everything is an Object), but cannot add, because do not know correct type (except null, which is any type)
![Page 23: Polymorphism & Methods COMP206 Geoff Holmes & Bernhard Pfahringer (these slides: lots of input from Mark Hall) poly + morphos (greek) “many forms”](https://reader035.fdocuments.net/reader035/viewer/2022062519/5697c00d1a28abf838cc981a/html5/thumbnails/23.jpg)
Bounded wildcards
public void drawAll(Collection<? extends Shape> c) {
for(Shape shape: c) {
shape.draw();
}
}
// again, only read access, allows collections of Shape or any sub type of Shape