09 polymorphism

15
Using IS-A and HAS-A LIS4930 © PIC When you want to know if one thing should extend another; apply the IS-A test. Triangle IS-A Shape. Cat IS-A Feline. Surgeon IS-A Doctor. YES YES YES Tub IS-A Bathroom. NO Bathroom HAS-A Tub. Tub and Bathroom are related, but not through inheritance: Bathroom HAS-A Tub instance variable. Bathroom has a reference to a Tub, but Bathroom does not extend Tub and vice-versa.

Upload: program-in-interdisciplinary-computing

Post on 22-Dec-2014

418 views

Category:

Documents


4 download

DESCRIPTION

 

TRANSCRIPT

Page 1: 09 polymorphism

LIS4930 © PIC

Using IS-A and HAS-AWhen you want to know if one thing should extend another; apply the IS-A test.

Triangle IS-A Shape.

Cat IS-A Feline.

Surgeon IS-A Doctor.

YES

YES

YES

Tub IS-A Bathroom.

NO Bathroom HAS-A Tub.

Tub and Bathroom are related, but not through inheritance: Bathroom HAS-A Tub instance variable.

Bathroom has a reference to a Tub, but Bathroom does not extend Tub and vice-versa.

Page 2: 09 polymorphism

LIS4930 © PIC

Canine

roam()

Wait! There’s More!Animal

makeNoise()eat()sleep()roam()

Wolf

makeNoise()eat()

If class B extends A, class B IS-A class A. This is true anywhere in the inheritance tree. If class C extends class B, class C passes the IS-A test for both B and A.Canine extends Animal

Wolf extends Canine

Wolf extends Animal

Canine IS-A Animal

Wolf IS-A Canine

Wolf IS-A Animal

Keep in mind that the inheritance IS-A relationship works in only ONE direction!

Page 3: 09 polymorphism

LIS4930 © PIC

Test Yourself!Oven extends Kitchen

Guitar extends Instrument

Person extends Employee

Ferrari extends Engine

FriedEgg extends Food

Beagle extends Pet

Container extends Jar

Metal extends Titanium

Beverage extends Coke

Page 4: 09 polymorphism

LIS4930 © PIC

Access Level Control

Mammal

private weightpublic colorprivate eat()private roam()public sleep()Elephant

makeNoise()

A subclass inherits all public instance variables and methods of the superclass, but does not inherit the private instance variables and methods of the superclass.public members are inheritedprivate members are not inherited

Page 5: 09 polymorphism

LIS4930 © PIC

Polymorphism! What is that?I think this is best explained with an example. Let’s step back

and look at the way we normally declare a reference and create an object…

1 Declare a reference variable

Dog myDog

myDog

Dog

Page 6: 09 polymorphism

LIS4930 © PIC

Polymorphism! What is that?

2 Create an object

Dog myDog = new Dog()

Dog

Dog object

Page 7: 09 polymorphism

LIS4930 © PIC

Polymorphism! What is that?

3 Link the object and the reference

Dog myDog = new Dog()

Dog

Dog object

myDog

Dog

Page 8: 09 polymorphism

LIS4930 © PIC

Polymorphism! What is that? Dog

Dog object

myDog

Dog

The important point is that the reference type AND the object type are the same.

Page 9: 09 polymorphism

LIS4930 © PIC

Polymorphism! What is that? Dog

Dog object

myDog

Animal

But with polymorphism, the reference and the object can be different.

Animal myDog = new Dog()

Page 10: 09 polymorphism

LIS4930 © PIC

Polymorphic ExampleAnimal[ ] animals = new Animal[5];

animals[0] = new Dog();

animals[1] = new Cat();

animals[2] = new Wolf();

animals[3] = new Hippo();

animals[4] = new Lion();

for(int i = 0; i < animals.length; i++){

animals[i].eat();animals[i].roam();

}

Page 11: 09 polymorphism

LIS4930 © PIC

You can have polymorphic arguments and return types

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);

}}

a

Animal

Page 12: 09 polymorphism

LIS4930 © PIC

Taking Advantage of Polymorphism

With polymorphism, you can write code that doesn’t have to change when you introduce new subclass types in the program.

That means if others want to take advantage of your Vet class, all they have to do is make sure their new Animal types extend class Animal.

Page 13: 09 polymorphism

LIS4930 © PIC

Keeping the Contract: Rules for Overriding

Appliance

boolean turnOn()

Toaster

boolean turnOn(int level)

1 Arguments must be the same, and return types must be compatibleThis is NOT an override.

Can’t change the arguments in an overriding method!This is actually a legal overLOAD, but not an overRIDE.

Page 14: 09 polymorphism

LIS4930 © PIC

Keeping the Contract: Rules for Overriding

Appliance

public boolean turnOn()

Toaster

private boolean turnOn()

2 The method can’t be less accessible.

NOT LEGAL! It’s not a legal override because we restricted the access level. Nor is it a legal overLOAD because we didn’t change arguments.

Page 15: 09 polymorphism

LIS4930 © PIC

Overloading A MethodMethod overloading is nothing more than having two methods with the same name but different argument lists. Period. It has nothing to do with inheritance and polymorphism. An overloaded method is NOT the same as an overridden method.

1 The return types can be different

2 You can’t change ONLY the return type

3 You can vary the access levels in any direction

public class Overloads { String uniqueID;

public int addNums(int a, int b) {return a + b;

} public double addNums(double a, double b) {

return a + b; }

public void setUniqueID(String ID){uniqueID = theID;

}

private void setUniqueID(int ssNumber){

String numString = “” + ssNumber;

setUniqueID(numString); }}