Lecture 9 - Inheritance & Polymorphism
Lecture 9 - Inheritance & Polymorphism
Lecture 9
The Institute of Finance Management
Faculty Of Computing, Information Systems & Mathematics
Computer Science & Mathematics Department
Superclasses and Subclasses
• Object-oriented programming allows you to define new classes
from existing classes.
• This is called inheritance.
• Inheritance is an important and powerful feature for reusing
software.
• Suppose you need to define classes to model circles,
rectangles, and triangles. These classes have many common
features.
• What is the best way to design these classes so as to avoid
redundancy and make the system easy to comprehend and
easy to maintain?
– The answer is to use inheritance.
Superclasses and Subclasses
• Inheritance enables you to define a general class (i.e., a
superclass) and later extend it to more specialized classes
(i.e., subclasses).
• You use a class to model objects of the same type.
• Different classes may have some common properties and
behaviors, which can be generalized in a class that can be
shared by other classes.
• You can define a specialized class that extends the
generalized class.
• The specialized classes inherit the properties and
methods from the general class.
Superclasses and Subclasses
• Consider geometric objects.
– Suppose you want to design the classes to model geometric
objects such as circles and rectangles.
– Geometric objects have many common properties and
behaviors.
– They can be drawn in a certain color and be filled or unfilled.
Thus a general class GeometricObject can be used to model all
geometric objects.
– This class contains the properties color and filled and their
appropriate getter and setter methods.
– Assume that this class also contains the dateCreated property
and the getDateCreated() and toString() methods. The toString()
Superclasses and Subclasses
• Since a circle is a special type of geometric
object, it shares common properties and
methods with other geometric objects.
– Thus it makes sense to define the Circle class that
extends the GeometricObject class.
– Likewise, Rectangle can also be defined as a
subclass of GeometricObject.
Superclasses and Subclasses
Superclasses and Subclasses
• A triangular arrow pointing to the superclass is used to
denote the inheritance relationship between the two
classes involved.
• In Java terminology, a class C1 extended from another class
C2 is called a subclass, and C2 is called a superclass.
• A superclass is also referred to as a parent class or a base
class, and a subclass as a child class, an extended class, or
a derived class.
• A subclass inherits accessible data fields and methods from
its superclass and may also add new data fields and
methods.
Superclasses and Subclasses
• TheCircle class inherits all accessible data
fields and methods from the GeometricObject
class.
– In addition, it has a new data field, radius, and its
associated getter and setter methods.
– The Circle class also contains the getArea(),
getPerimeter(), and getDiameter() methods for
returning the area, perimeter, and diameter of the
circle.
Superclasses and Subclasses
• The Rectangle class inherits all accessible data
fields and methods from the GeometricObject
class.
– In addition, it has the data fields width and height
and their associated getter and setter methods.
– It also contains the getArea() and getPerimeter()
methods for returning the area and perimeter of
the rectangle.
Superclasses and Subclasses
• The keyword extends tells the compiler that
the Circle class extends the GeometricObject
class, thus inheriting the methods getColor,
setColor, isFilled, setFilled, and toString.
TestCircleRectangle.java
Superclasses and Subclasses
• Note the following points regarding inheritance
– Contrary to the conventional interpretation, a
subclass is not a subset of its superclass. In fact, a
subclass usually contains more information and
methods than its superclass.
– Private data fields in a superclass are not
accessible outside the class. Therefore, they
cannot be used directly in a subclass. They can,
however, be accessed/mutated through public
accessors/mutators if defined in the superclass.
Using the super Keyword
• The keyword super refers to the superclass and can
be used to invoke the superclass’s methods and
constructors.
• The keyword super refers to the superclass of the
class in which super appears. It can be used in two
ways:
– To call a superclass constructor.
super(), or super(parameters);
– To call a superclass method.
super.method(parameters);
Overriding Methods
• A subclass inherits methods from a superclass.
Sometimes it is necessary for the subclass to
modify the implementation of a method
defined in the superclass.
• This is referred to as method overriding
Overriding Methods
• The toString method in the GeometricObject
class returns the string representation of a
geometric object.
• This method can be overridden to return the
string representation of a circle. To override it,
add the following new method in the Circle
class.
Overriding Methods
Overriding Methods
Overriding Methods
Polymorphism
• Polymorphism means that a variable of a
supertype can refer to a subtype object
• What are subtype and supertype?
• A class defines a type. A type defined by a
subclass is called a subtype, and a type defined
by its superclass is called a supertype.
• Therefore, Circle is a subtype of GeometricObject
and GeometricObject is a supertype for Circle.
Polymorphism
• Polymorphism is an object-oriented
programming concept that refers to the ability
of a method to take on different
forms/behaviors depending on the object
type.
• The word ‘polymorphism’ literally means ‘a
state of having many shapes’ or ‘the capacity
to take on different forms’
Polymorphism
Polymorphism Example
• Example:
– Animal.java (Animal make sound)
– Dog.java (Dog make its own sound)
– Cat.java (Cat make its own sound)
Polymorphism Example
Animal.java
Dog.java
Cat.java
SimplePolyDemo.jav
Polymorphism: Binding
• Polymorphism in Java has two types:
1. Compile time polymorphism (static binding)
2. Runtime polymorphism (dynamic binding).
• Method overloading is an example of static
polymorphism
• Method overriding is an example of dynamic
polymorphism.
Polymorphism: Static Binding
• In Java, static polymorphism is achieved through method
overloading.
• Method overloading means there are several methods
present in a class having the same name but different
types/order/number of parameters.
• At compile time, Java knows which method to invoke by
checking the method signatures.
Declared Actual
type type
• The actual type of the variable is the actual class for the object
referenced by the variable
• It should be noted that in the first call to move(), the reference
type is Vehicle and the object being referenced is MotorBike.
Polymorphism – Dynamic Binding
// create vehicle class
class Vehicle{
public void move(){
System.out.println(“Vehicles can move!!”);
}
}
// Test programe
class Test{
public static void main(String[] args){