Agenda & Reading
Topics:
Polymorphism Example
COMPSCI 230
Polymorphism
Reading
Person HouseWife KFCChef PizzaChef TakeAwayChef Array of Objects
Java 2 The Complete Reference, Chapter 8,
Dynamic Method Dispatch, page 211-216 Inheritance
The Java Tutorial
http://java.sun.com/docs/books/tutorial/java/IandI/subclasses.html http://java.sun.com/docs/books/tutorial/java/IandI/override.html
Overriding and Hiding Methods
Exercise 4
2 Questions
COMPSCI 230
Handout 04
Polymorphism
Definition:
Example
Our COMPSCI230Kitchen has a Person
Polymorphism is the ability of objects belonging to different types to respond to method calls of methods of the same name, each one according to an appropriate type-specific behaviour. The program does not have to know the exact type of the object in advance, so this behavior can be implemented at run time (this is called late binding or dynamic binding). Classes:
Person
A person has surname and first name Create a person in our COMPSCI230Kitchen
Person p = new Person();
Instance variables: surname and first name Methods: getName() get surname and first name HouseWife
cookDinner() to prepare food for dinner
Person
Kitchen
getName cookDinner
Example:
An ordinary person prepares dinner using microwave!
Kitchen Person HouseWife PizzaChef KFCChef TakeAwayChef
public class Person { String surname; String firstname; ... public void cookDinner() { System.out.println("Microwave Dinner"); } }
KFCChef TakeAwayChef
PizzaChef
Person p = new Person(); System.out.println(p.getName()); p.cookDinner();
COMPSCI 230
Handout 04
COMPSCI 230
Handout 04
HouseWife
Now our COMPSCI230Kitchen has a houseWife person HouseWife p = new ___________
PizzaChef
Now our COMPSCI230Kitchen has a PizzaChef person PizzaChef p = ______________
Inherited method: getName
To get her name To clean our kitchen
Additional method: cleanKitchen Overridden method: cookDinner
To cook food for dinner A housewife makes Roast chicken with Potato.
Person HouseWife
Inherited method: getName
To get her name
Person PizzaChef
System.out.println(p.getName()); p.cookDinner();
getName cookDinner cleanKitchen
Overridden method: cookDinner
To cook food for dinner A pizzachef makes Pizza
public class HouseWife extends Person { public HouseWife(String firstname, String surname) { super(firstname, surname); } public void cookDinner() { System.out.println("Roast Chicken with Potato"); } public void cleanKitchen() { System.out.println("Cleaning now"); } }
System.out.println(p.getName()); p.cookDinner();
public class PizzaChef extends Person { public PizzaChef(String firstname, String surname) { super(firstname, surname); } public void cookDinner() { System.out.println("Pizza"); } }
COMPSCI 230
Handout 04
COMPSCI 230
Handout 04
KFCChef
Now our COMPSCI230Kitchen has a KFCChef person KFCChef p = _________________
TakeAwayChef
Now our COMPSCI230Kitchen has a TakeAwayChef person p = ________________ TakeAwayChef
Inherited method: getName
To get her name
Person kFCChef
System.out.println(p.getName()); p.cookDinner();
Inherited method: getName
To get her name
Person TakeAwayChef
System.out.println(p.getName()); p.cookDinner();
Overridden method: cookDinner
To cook food for dinner A KFCChef makes KFC fried chicken
Overridden method: cookDinner
To cook food for dinner A TakeAwaychef makes fried rice
public class KFCChef extends Person { public KFCChef(String firstname, String surname) { super(firstname, surname); } public void cookDinner() { System.out.println("KFC Chicken"); } }
public class TakeAwayChef extends Person { public TakeAwayChef(String firstname, String surname) { super(firstname, surname); } public void cookDinner() { System.out.println("Fried Rice"); } }
COMPSCI 230
Handout 04
COMPSCI 230
Handout 04
Example:Kitchen.java
An array of Person
The array holds five objects of type Person. Because of their inheritance relationship with the Person class, the HouseWife, PizzaChef, KFCChef and TakeAwayChef classes can be assigned to the array. Within the for-loop, the cookDinner method is invoked on each element of the array.
Rules
Polymorphism is the ability of objects belonging to different types to respond to method calls of methods of the same name, each one according to an appropriate type-specific behaviour.
At Compile time, At Run time,
At first, when a method call is executed, the object is ________ first and if the method does not exist, then check the superclass (work from the child to the parent class) Type checking is done at compile time Next, the method is _________ based on the object, not on the type of variable. The derived classes override the cookDinner method of the Person class. This makes the overridden cookDinner() methods of the derived classes execute when the cookDinner() method is called using the base class reference from the array
the type of each variable is _______ to ensure that only methods
belonging to that type are called of the variable.
the code executed depends on the _______ of the object, not the type
HouseWife w; w = new HouseWife("Theresa", "Thompson"); w.cookDinner(); w.cleanKitchen(); Person p1; p1 = new HouseWife("Theresa", "Thompson"); p1.cookDinner(); //p1.cleanKitchen(); //Compile error No cleanKitchen method declared in Person class.
Person[] pList = new Person[5]; pList[0] = new Person("Dick", "Smith"); pList[1] = new HouseWife("Theresa", "Thompson"); pList[2] = new PizzaChef("Michael", "Hill"); pList[3] = new KFCChef("Peter", "Wong"); pList[4] = new TakeAwayChef("Kevin", "Chan"); for (int i=0; i<pList.length; i++) pList[i].cookDinner();
The result depends on the object stored, not on the type of the variable.
COMPSCI 230
Handout 04
COMPSCI 230
Handout 04
10