Vous êtes sur la page 1sur 48

Week 08

Inheritance and Polymorphisms


Learning Outcomes

•Upon completion of this lecture, student


should be able to:
• To implement a subclass from a superclass
through inheritance
• To invoke the superclass’s constructors and
methods using the super keyword
• To override instance methods in the subclass
• To distinguish differences between overriding and
overloading
• To restrict access to data and methods to
subclasses using the protected visibility modifier
Contents

• What Is Inheritance?
• Calling the Superclass Constructor
• Overriding Superclass Methods
• Access Control Modifiers
• Chains of Inheritance
• The Object Class
• Polymorphism
• Casting Object
Inheritance (Is-A relationship)

Insect
Contains those attributes
and methods that are
shared by all insects.

BumbleBee Grasshopper

Contains those attributes and Contains those attributes and


methods that specific to a Bumble methods that are specific to a
Bee. Grasshopper.

11-4
Inheritance
Inheritance
Inheritance
Inheritance

• The Java keyword, extends, is used on the class


header to define the subclass.

<modifier> class <name> extends <superclass>


{
…….
}

public class Manager extends Employee {

11-8
Constructor
Constructor
Constructor
Recall
Overriding
Overriding Methods
Rule of Overriding
Overriding Methods
Question

Overloading Vs Overriding

11-18
final keyword

• The final modifier will prevent the overriding of a


superclass method in a subclass.

public final void message()

• If you do not want a class to be subclassed, precede the


class declaration with the keyword final.
• If you do not want a method to be overridden by a
subclass, precede the method declaration with the
keyword final.
• If you want a variable to be read-only (that is, a
constant), precede it with the keyword final.
11-20
Example of final

final class MyClass {


final int x = 3;
public static final double PI = 3.14159;

final double getPI() { return PI; }


}
The Object Class
Some Methods in the Object Class

Method Purpose

Object clone( ) Creates a copy of this object


boolean equals(Object obj) Tests whether two objects are equal
void finalize( ) Called before recycling the object
Class<?> getClass( ) Returns the class of the object
int hashCode( ) Returns the hash code of the object
void notify( ) Resumes execution of a thread waiting on the
object
void notifyAll( ) Resumes execution of all threads waiting on
the object
String toString( ) Returns a string describing the object
void wait( ) Waits on another thread of execution
The equals Method
An equals example
An equals example
Practice 1
Chains of Inheritance

• A superclass can also be derived from another class.

Object

GradedActivity

PassFailActivity

PassFailExam

11-31
Chains of Inheritance

• Classes often are depicted graphically in a class


hierarchy.
• A class hierarchy shows the inheritance
relationships between classes.
GradedActivity

FinalExam PassFailActivity

PassFailExam

11-32
Polymorphism

Employee e = new Employee ();


Employee e1= new Manager();

Employee e2= new Engineer ();

Employee e3= new Secretary();


Virtual Method Invocation
GradedActivity exam = new GradedActivity();;

GradedActivity exam = new FinalExam(50, 7);

GradedActivity exam2 = new PassFailActivity(70);

GradedActivity exam3 = new PassFailExam(100, 10, 70);

In Java, a reference variable is polymorphic because it can reference


objects of types different from its own, as long as those types are
subclasses of its type.
• The GradedActivity class has three methods: setScore,
getScore, and getGrade. GradedActivity
- score : double
+ setScore(s : double) : void
+ getScore() : double
+ getGrade() : char

• A GradedActivity variable can be used to call only those


three methods.
GradedActivity exam = new PassFailExam(100, 10, 70);
System.out.println(exam.getScore());
System.out.println(exam.getGrade());
System.out.println(exam.getPointsEach());
ERROR!

11-37
Polymorphism and Dynamic
Binding

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

• Java performs dynamic binding or late binding when a variable contains


a polymorphic reference.
• The Java Virtual Machine determines at runtime which method to call,
depending on the type of object that the variable references.

11-38
Casting Objects
Inheritance AND Polymorphism
Main Reference :
I. Savitch, W. Absolute Java. 4th Edition, Pearson International Edition.
2010.
II. Tony Gaddis. “Starting Out with Java: From Control Structures
through Objects”. 4th Edition. Addison-Wesley. 2010
III. Y. Daniel Liang. “Introduction to Java Programming, Comprehensive”.
8th Edition. Prentice Hall. 2011
Lecturer: Tan Soo Fun
KS20603 Object-Oriented Programming
The End

Vous aimerez peut-être aussi