Chapter 3
“Polymorphism
and Inheritance”
Outline
• Understanding Inheritance
• Inheritance Diagrams
• Constructors in Derived Classes
• Type Compatibility
• Polymorphism
• Method Overriding
• The Object Class
• Abstract Classes and Methods
• Revisiting Polymorphism
Object-Oriented Programming
(OOP)
• Three key guiding principles
– Encapsulation
– Inheritance
– Polymorphism
Inheritance
• The ability of a class to reuse the status
and behavior of another class while
adding its own functionality.
– The status of a class is the collection of its
attributes (or fields).
– The behavior of a class is the collection of
its methods.
Inheritance Example
• Class Person
– Attributes: Name
– Methods:
• Constructors for Person
• setName( … )
• getName ( )
• writeOutput ( )
• hasSameName ( )
Inheritance Example
• Class Student inherits from Person
– Able to use existing Person functionality
• It has all of its attributes and methods.
– But incorporates additional functionality
• Attributes: studentNumber
• Methods:
– Constructors for Student
– reset ( …)
– getStudentNumber( ), setStudentNumber( … )
– equals (… )
Inheritance Example
• Person is the base (or parent) class
• Student is the derived (or child) class
• Base classes are more general.
• Derived classes are more specific.
• Inheritance greatly enhances the ability to
reuse existing code.
– This simplifies the development time.
– And makes design much simpler and cleaner.
Inheritance Example
• In Java, a derived class can only have a
single base class.
– Other languages such as C++ allows
inheritance from multiple classes.
• Syntax:
class <derived> extends <base>
Private Class Members
• Private class members (i.e. attributes and
methods) are NOT inherited.
• Thus, cannot be manipulated by the
derived classes.
– Because they are “private” to the base class.
• Likewise, private methods in the base
class cannot be used in the derived class.
• Unless declared as “public” or “protected”
Derived Classes
A class hierarchy
Inheritance Diagrams
Inheritance Diagrams
Constructors in Derived Classes
• A derived class does NOT inherit the
constructors from the base class
Constructor in a derived class must invoke
constructor from base class
• Use the reserved word super
Must be first action in the child constructor
The this keyword - Again
• Also possible to use the this keyword
Use to call any constructor in the class
• When used in a constructor, this calls
constructor in the same class
Unlike super, which invokes the constructor
of the base class
Programming Example
• A derived class of a derived class
• View
class Undergraduate
• Has all public members of both
Person
Student
• This reuses the code in superclasses
Programming Example
More details
of the
class
hierarchy
Type Compatibility
• In the class hierarchy
Each Undergraduate is also a Student
Each Student is also a Person
• An object of a derived class can serve as
an object of the base class
Note this is NOT typecasting
• An object of a child class can be
referenced by a variable of an ancestor
type
Type Compatibility
• Be aware of the "is-a" relationship
A Studentis a Person
This is the basis for polymorphism
• Another relationship is the "has-a"
A class can contain (as an instance variable)
an object of another type
If we specify a date of birth variable for
Person – it "has-a" Date object
Polymorphism
• From the Greek
– “poly” = many
– “morph” = form, figure, silhouette
• The ability of a class method to do
different things based on the object it is
acting upon.
– Example: Animal.makeNoise();
– Dog: barks
– Cat: mews
Polymorphism
Two types of polymorphism
1. Method Overloading:
• Multiples implementations of the same method
occur in the same class.
• Each differs in the number and types of the method
arguments.
• Java invokes the closest one that matches the
actual arguments being passed to the method.
– System.out.println ( int );
– System.out.println( char );
– System.out.println ( String ) ;
– System.out.println ( boolean );
Polymorphism
Two types of polymorphism
2. Method Overriding:
• Multiples implementations of the same method
occur in different classes along the same hierarchy.
• A child class “overrides” the implementation of a
method provided by its base class.
• Examples:
– Cat.makeNoise( ) overrides Animal.makeNoise( )
– SiameseCat.makeNoise( ) overrides Cat.makeNoise( )
Overriding vs. Overloading
• Do not confuse overriding with overloading
Overriding takes place in the subclass – new
method with same signature
Overloading takes place in the same class –
new method with different signature
The final Modifier
• It is possible to specify that a method
cannot be overridden in a subclass
• Add modifier final to the method heading
public final void specialMethod()
• An entire class may be declared final
Thus cannot be used as a base class to derive
any other class
Calling an Overridden Method
• Reserved word super can also be used to
call a base class method in the derived
implementation
• Useful if the derived implementation is an
extension of the base (overridden) one.
The Object Class
• Java has a class that is the ultimate
ancestor of every class
The class Object
• Thus, it is possible to write a method with
formal parameter of type Object
Actual parameter in the call can be object of
any type
• Example: method
println(Object theObject)
The Object Class
• The Object class has some methods that
every Java class inherits
• Examples
The equals()method
The toString()method
• The toString() method is called when
println(theObject)is invoked
Better to define your own toString to handle
this.
A Better equals Method
• The programmer of a class should override
the equals() method of Object
• View code of sample override
public boolean equals (Object
theObject)
Abstract Classes and Methods
• So far, our base classes provide an
implementation for all of its methods.
– This means that objects from base classes can
actually be created.
– e.g. Person p = new Person( );
• What if we cannot provide an
implementation for a particular method?
– e.g. Animal.makeNoise();
Abstract Classes and Methods
• We declare the method as abstract.
– No implementation is provided in the base
class.
– Forces any derived class to implement the
method.
– Notice the semicolon at the end, meaning that
the method has no body.
– Different from a method with an empty body.
Abstract Classes and Methods
• A class with at least one abstract method
becomes an abstract class.
• No objects can be created out of an
abstract class.
– You will derive other classes from it.
– And create objects of the derived classes.
– e.g. no Animal objects.
– but of type Cat, Dog, Bird, Moose, etc.
Abstract Classes and Methods
Extending Abstract Classes
Implement
s the
abstract
method
Extending Abstract Classes
Revisiting Polymorphism
• Method overriding is one of the major
sources of polymorphism.
• How does it work?
– The base class declares a method.
– The derived class redefines (overrides) the
method.
• Must have the same signature.
Revisiting Polymorphism
• A variable of any base class can “hold” an
object of any of its derived classes.
– Functionality is restricted to what the base
class can “see” in the derived class.
• All members of the base class.
• Overridden methods in the derived class.
• The implementation in the derived class
prevails when the overridden method is
invoked.
– Called “late binding”
An Example
• a2 declared as Animal (base class)
• But created as Cat (derived class)
• Therefore, “late bound” to Cat at runtime.
Another Example
Another Example
Cat.
makeNoise( )
SiameseCat.
makeNoise( )
An Even More Powerful Example
• Suppose the following classes have been
created:
• Dog extends Animal
• Overrides makeNoise( ) as “Wof Wof Wof”
• Cow extends Animal
• Overrides makeNoise( ) as “Mooooooooo”
• Birdie extends Animal
• Overrides makeNoise( ) as “Tweet Tweet”
An Even More Powerful Example
An Even More Powerful Example
This is the program output:
End of CH_3