420-N22_LA
Object Oriented
Programming Level 2
Haikel Hichri
Professor of Computer Science
Department of Computer Science and technology
Champlain College – Saint Lambert
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020
Pearson Education, Inc. All rights reserved.
1
Polymorphism
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020
Pearson Education, Inc. All rights reserved.
2
Polymorphism
F A reference variable can reference objects of classes that are
derived from the variable’s class.
GradedActivity exam;
F We can use the exam variable to reference a
GradedActivity object.
exam = new GradedActivity();
F The GradedActivity class is GradedActivity
also used as the superclass for the
FinalExam class.
FinalExam PassFailActivity
F An object of the FinalExam
class is a GradedActivity
object. PassFailExam
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020
Pearson Education, Inc. All rights reserved.
Polymorphism
F A GradedActivity variable can be used to reference a FinalExam
object!
GradedActivity exam = new FinalExam(50, 7);
F This statement creates a FinalExam
object and stores the object’s address in the
exam variable. GradedActivity
F This is an example of polymorphism.
F The term polymorphism means the ability to
take many forms. FinalExam PassFailActivity
F In Java, a reference variable is polymorphic
because it can reference objects of types
different from its own, as long as those PassFailExam
types are subclasses of its type.
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020
Pearson Education, Inc. All rights reserved.
Polymorphism
F Other legal polymorphic references:
GradedActivity exam1 = new FinalExam(50, 7);
GradedActivity exam2 = new PassFailActivity(70);
GradedActivity exam3 = new PassFailExam(100, 10, 70);
GradedActivity
FinalExam PassFailActivity
PassFailExam
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020
Pearson Education, Inc. All rights reserved.
Array of different types
GradedActivity[] tests = new GradedActivity[3];
F Given this code: tests[0] = new GradedActivity();
tests[1] = new PassFailExam(20, 5, 60);
tests[2] = new FinalExam(50, 7);
PassFailExam FinalExam
GradedActivity Object
Object
Object
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020
Pearson Education, Inc. All rights reserved.
6
Polymorphism
F You cannot assign a superclass object to a
subclass reference variable.
FinalExam exam1 = new GradedActivity();
PassFailActivity exam2 = new GradedActivity();
ALL
PassFailExam exam3 = new GradedActivity(); WRONG!
PassFailExam exam4 = new PassFailActivity();
FinalExam exam4 = new GradedActivity();
GradedActivity
FinalExam PassFailActivity
PassFailExam
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020
Pearson Education, Inc. All rights reserved.
7
Polymorphism
F The GradedActivity class has three methods: setScore, getScore,
and getGrade.
F A GradedActivity variable can be used to call only those three methods.
GradedActivity exam = new PassFailExam(100, 10, 70);
System.out.println(exam.getScore()); // This works.
System.out.println(exam.getGrade()); // This works.
System.out.println(exam.getPointsEach()); // ERROR!
GradedActivity
F The GetPointsEach method
belongs to PassFailExam
class. FinalExam PassFailActivity
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020
PassFailExam
Pearson Education, Inc. All rights reserved.
Polymorphism and Dynamic
Binding
F If the object of the subclass has overridden a method in the
superclass:
– If the variable makes a call to that method the subclass’s
version of the method will be run.
GradedActivity exam = new PassFailActivity(60);
exam.setScore(70);
System.out.println(exam.getGrade());
GradedActivity
+getGrade():char
PassFailActivity
+getGrade():char
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020
Pearson Education, Inc. All rights reserved.
Polymorphism and Dynamic
Binding
F Java performs dynamic binding or late binding when a variable
contains a polymorphic reference.
F The Java Virtual Machine determines at runtime which method to
call, depending on the type of object that the variable references.
GradedActivity exam = new PassFailActivity(60);
exam.setScore(70);
System.out.println(exam.getGrade());
If the variable makes a call to that method the
subclass’s version of the method will be run.
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020
Pearson Education, Inc. All rights reserved.
Polymorphism
F It is the object’s type, rather than the
reference type, that determines which
method is called.
F Example:
– Polymorphic.java
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020
Pearson Education, Inc. All rights reserved.
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020
Pearson Education, Inc. All rights reserved.
12
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020
Pearson Education, Inc. All rights reserved.
13