chapter6_polymorphism
chapter6_polymorphism
CHAPTER 6
Polymorphism
3
Polymorphism
1. What is polymorphism?
2. Why polymorphism?
3. Specific uses of polymorphism
4. Notes
5. Remember
6. Casting
7. Instance of Operations
8. Remember
4
+getClassName():String
GraduateStudent
+getClassName():String
6
//Polymorphic method
public static void display(Student stu){
System.out.println(stu.getClassName());
}
}
10
Student getClassName():String
GraduateStudent
getClassName():String
getClassName():String
Parent/Superclass Children/Subclass
11
System.out.println(s[i].getClassName());
}
}
}
15
Note #1 (1)
• Look at the codes again.
public class Student{ GraduateStudent and
public String getClassName(){ UndergraduateStudent
return "Student Class"; override getClassName()
} method!
}
public class GraduateStudent extends Student{
@Override public String getClassName(){
return "GraduateStudent Class";
}
}
public class UndergraduateStudent extends Student{
@Override public String getClassName(){
return “UndergraduateStudent Class";
}
}
16
Note #1 (2)
• Please note that the two subclasses of Student override
getClassName().
Then, we can use polymorphism to refer to getClassName() in
the sub class.
public class TestPoly1{
public static void main(String[] args){
Student s;
s = new GraduateStudent();
System.out.println(s.getClassName());
s = new UndergraduateStudent();
System.out.println(s.getClassName());
}
}
17
Note #2 (1)
GraduateStudent, we add a
• But now in a subclass
Note #2 (2)
• And then we want to use polymorphism to call
getDegree() in the subclass,
public class TestPoly1{
public static void main(String[] args){
Student s = new GraduateStudent();
System.out.println(s.getDegree());
}
}
It will show a compile error because Student class
doesn’t have getDegree().
19
Note #3 (1)
• What about if we want add a method, say getID() to
the parent, Student.
public class Student{
public String getClassName(){
return "Student Class";
}
public String getID(){
return “NPIC001";
}
}
20
Note #3 (2)
• Then we want to call getID().
public class TestPoly1{
public static void main(String[] args){
Student s = new GraduateStudent();
System.out.println(s.getID());
}
}
Q. Is it a compile error?
A. No. As long as getID() is in Student, we can call
it without any question.
21
Student getClassName():String
GraduateStudent
getClassName():String
getClassName():String
Remember (1)
• Polymorphism is a mechanism to enable us to choose a
method at runtime dynamically.
• Polymorphism always needs Inheritance.
• Polymorphism always needs Overridden Methods.
• There is no keyword to indicate Polymorphism.
• You can recognise it whenever you see a class type that is
different from its constructor. E.g.
Student s = new GraduateStudent();
• You can also recognise it, when a class type is Object
class. E.g.
private void aMethod(Object ob){...}
24
Remember (2)
• Polymorphism is not as vital as inheritance. That is,
without polymorphism, you still CAN make a Java
program; however without inheritance, especially when
using Java library code, you CANNOT achieve very much.
25
CB: Curious
Ball
f._ _ _ ?
SR: Then you can call only those methods from Fruit
class like isEatable() and getColor(), and, of
course, other 9 methods from Object class.
29
Remember (1)
• Casting is a technique to convert a variable from one
class type to another one within an inheritance hierarchy.
That is, if we cast a class to another class in different root,
you will get run-time error. E.g:
Fruit fruit = new Grape();
Grape grape = (Grape) fruit;// OK
Apple apple = (Apple) fruit;//Runtime Error
42
Remember (2)
• A keyword instanceof is an operation to check whether a
class that we want to cast is in the correct hierarchy or
not.
• E.g.:
Fruit fruit = new Grape();
if(fruit instanceof Grape){
Grape grape = (Grape) fruit;
} else if(fruit instanceof Apple){
Apple apple = (Apple) fruit;
}
fruit.getColor();
fruit.getTaste();