Vous êtes sur la page 1sur 10

Inheritance and

Polymorphism
• Class Hierarchy
• Restrictions on Overriding
• Method Lookup
• Inheritance and Constructor
• The Superclass in Object Construction
• this and super
• Polymorphism
Prepared by: Dexie C. Gaspar 1
Class Hierarchies Object

Equals()

Object Number

extends byteValue()
doubleValue()
Number
intValue()
extends :
Integer

extends Integer
Integer zero = new Integer(0);
If(zero.equals(x))
byte = zero.byteValue();
Prepared by: Dexie C. Gaspar 2
Restrictions on Method Overriding
–The parameter list and return type must
match the inherited method exactly
–The access modifier must not be more
restrictive than that of the inherited
method
For example, if overriding a protected method, the
new method can be protected or public, but not
private
Prepared by: Dexie C. Gaspar 3
Method Lookup
•When a method is invoked, the lookup begins in
the object's class definition
•If the method is not found in the object's own
class, the search continues in the superclass
and up the hierarchy until it is found
•When the method is found, it will be invoked on
the object to which the method was passed
•If the method was never implemented in any of
the classes in the hierarchy, an error is issued at
compile time Prepared by: Dexie C. Gaspar 4
Inheritance and Constructors
•Only constructors within the class being
instantiated and within the immediate superclass
can be invoked
•Constructors in the superclass are invoked with
the keyword super and the parameter list
–the parameter list must match that of an existing
constructor in the superclass
•Constructors in the same class are invoked with
the keyword this and the parameter list
•The first line of your constructor can be one of:
– super(…);
– this(…);
Prepared by: Dexie C. Gaspar 5
The Superclass in Object Construction
•Superclass objects are built before the subclass
–The compiler supplies an implicit super() call for all
constructors
•super(…) initializes superclass members
•If the first line of your constructor is not a call to
another constructor, super() is called
automatically
–Zero argument constructor in the superclass is called
as a
result
–This can cause an error if the superclass does not have
a
zero-argument constructor
Prepared by: Dexie C. Gaspar 6
More on this and super
•this and super can be used in instance
methods and constructors in Java
–They cannot be used in class (static) methods
•The keyword super lets you use code from a
superclass when you are overriding a method
•this and super affect method look-up
–this: method look-up starts in the current class
–super: method look-up starts in the immediate
superclass

Prepared by: Dexie C. Gaspar 7


Polymorphism
– The ability of a reference variable to
change behavior according to what object
it is holding.
– This allows multiple objects of different
subclasses to be treated as objects of a
single superclass, while automatically
selecting the proper methods to apply to a
particular object based on the subclass it
belongs to.
Prepared by: Dexie C. Gaspar 8
Polymorphism
Given the parent class Person and the
subclass Student and another subclass of
Person which is Employee.
Below is the class hierarchy for that

Person getName()

Student Employee

overrides: overrides:
getName() Prepared by: Dexie C. Gaspar getName() 9
Polymorphism
In Java, we can create a reference that is of
type superclass to an object of its
subclass.

public static main( String[] args ) {
Person ref;
Student studentObject = new Student();
Employee employeeObject = new Employee();
ref = studentObject; 
ref.getName();
ref = employeeObject;
ref.getName();
} Prepared by: Dexie C. Gaspar 10

Vous aimerez peut-être aussi