More on Polymorphism
-
Upload
amrutesh-ghadham -
Category
Documents
-
view
215 -
download
0
Transcript of More on Polymorphism
-
7/30/2019 More on Polymorphism
1/3
COMPSCI 230
Polymorphism
COMPSCI 230 Handout 04 2
Agenda & ReadingTopics: Polymorphism Example
Person
HouseWife KFCChef
PizzaChef TakeAwayChef Array of Objects
Reading Java 2 The Complete Reference, Chapter 8,
Dynamic Method Dispatch, page 211-216
The Java Tutorial Inheritance
http://java.sun.com/docs/books/tutorial/java/IandI/subclasses.html
Overriding and Hiding Methods http://java.sun.com/docs/books/tutorial/java/IandI/override.html
Exercise 4 2 Questions
COMPSCI 230 Handout 04 3
PolymorphismDefinition: Polymorphism is the ability of objects belonging to different types to
respond to method calls of methods of the same name, each oneaccording to an appropriate type-specific behaviour. The program
does not have to know the exact type of the object in advance, sothis behavior can be implemented at run time (this is called latebinding or dynamic binding).
Example: Classes:
Kitchen
Person
HouseWife
PizzaChef
KFCChef TakeAwayChef
COMPSCI 230 Handout 04 4
ExampleOur COMPSCI230Kitchen has a Person A person has surname and first name
Create a person in our COMPSCI230Kitchen
Person Instance variables: surname and first name
Methods: getName() get surname and first name cookDinner() to prepare food for dinner
An ordinary person prepares dinner using microwave!
Person
HouseWife
KFCChef
PizzaChef TakeAwayChef
Kitchen
public class Person {
String surname;
String firstname;
...
public void cookDinner() {
System.out.println("Microwave Dinner");
}
}
Person p = new Person();
System.out.println(p.getName());
p.cookDinner();
Person p = new Person();
getNamecookDinner
-
7/30/2019 More on Polymorphism
2/3
COMPSCI 230 Handout 04 5
HouseWifeNow our COMPSCI230Kitchen has a houseWife personHouseWife Inherited method: getName
To get her name Additional method: cleanKitchen
To clean our kitchen
Overridden method: cookDinner To cook food for dinner A housewife makes Roast chicken with Potato.
p = new ___________
getNamecookDinner
cleanKitchen
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();
Person
HouseWife
COMPSCI 230 Handout 04 6
PizzaChefNow our COMPSCI230Kitchen has a PizzaChef person
PizzaChef Inherited method: getName
To get her name
Overridden method: cookDinner To cook food for dinner
A pizzachef makes Pizza
Person
PizzaChef
p = ______________
public class PizzaChef extends Person {
public PizzaChef(String firstname, String surname) {
super(firstname, surname);
}
public void cookDinner() {
System.out.println("Pizza");
}
}
System.out.println(p.getName());
p.cookDinner();
COMPSCI 230 Handout 04 7
KFCChefNow our COMPSCI230Kitchen has a KFCChef person
KFCChef Inherited method: getName
To get her name
Overridden method: cookDinner To cook food for dinner
A KFCChef makes KFC fried chicken
Person
kFCChef
p = _________________
public class KFCChef extends Person {
public KFCChef(String firstname, String surname) {
super(firstname, surname);
}
public void cookDinner() {
System.out.println("KFC Chicken");
}
}
System.out.println(p.getName());
p.cookDinner();
COMPSCI 230 Handout 04 8
TakeAwayChefNow our COMPSCI230Kitchen has a TakeAwayChef person
TakeAwayChef Inherited method: getName
To get her name Overridden method: cookDinner
To cook food for dinner
A TakeAwaychef makes fried rice
Person
TakeAwayChef
p = ________________
public class TakeAwayChef extends Person {
public TakeAwayChef(String firstname, String surname) {
super(firstname, surname);
}
public void cookDinner() {System.out.println("Fried Rice");
}
}
System.out.println(p.getName());
p.cookDinner();
-
7/30/2019 More on Polymorphism
3/3
COMPSCI 230 Handout 04 9
An array of PersonThe 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 eachelement of the array. At first, when a method call is executed, the object is ________ first
and if the method does not exist, then check the superclass (workfrom the child to the parent class) Type checking is done atcompile time
Next, the method is _________ based on the object, not on the typeof variable. The derived classes override the cookDinner method ofthe Person class. This makes the overridden cookDinner() methodsof the derived classes execute when the cookDinner() method iscalled using the base class reference from the array
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