Chapter 6:
Polymorphism
CSC435: OBJECT ORIENTED PROGRAMMING
Prepared by Siti Norsyuhada Zainudin
Ammended from Bakri & Khairunnisa
Content
Polymorphism concept
Abstract classes and methods
Method overriding
Array of super classes
Concrete sub classes and methods
Polymorphism Concept
▶ Polymorphism means “having many forms”.
▶ Polymorphism allows a single variable to refer to objects from
different subclasses in the same inheritance hierarchy.
❖ The ability of a variable to have more than one type –hold
values of different types.
❖ the ability to use the same variable to refer to methods that
perform different tasks.
Polymorphism Concept cont.
▶ For example, if Student and Staff are subclasses of Person, then the
following statements are valid:
Person
Student Staff
Polymorphism Concept
▶ When the variable (example: person[i]) is used with “dot notation”,
obj_reference.method() to invoke a method, exactly which
method is run depends on the object that the obj_reference
currently refer to (NOT that object reference itself).
Polymorphism
Example
Superclass :
PersonPoly
Polymorphism
Example
Subclass :
StudentPoly
Polymorphism
Example
Subclass:
EmployeePoly
Advantages Polymorphism
Programs
are easier
Powerful
Flexibility. to modify,
mechanism.
extend and
reuse
Content
Polymorphism concept
Abstract classes and methods
Method overriding
Array of super classes
Concrete sub classes and methods
Abstract Classes
▶ Class design should ensure that a superclass contains common features
of its subclasses.
▶ Sometimes a superclass is so abstract that it cannot have any specific
instances.
▶ Such a class is referred to as an abstract class.
▶ Its purpose is to be a parent to several related classes and the child
classes inherit from the abstract parent class.
▶ Abstract classes are used to provide a template or design for concrete
subclasses down the inheritance tree.
Abstract Classes
How?
Create a superclass that only defines a
generalized form that will be shared by all Vehicle
of its subclasses, leaving it to each subclass
to fill in the details.
Such a class determines the nature of the
methods that the subclasses must
implement.
One way this situation can occur is when a
superclass is unable to create a
meaningful implementation for a method.
Abstract Classes
▶ ABSTRACT CLASS is
a class defined with the modifier abstract, or
a class that contains an abstract method, or
a class does not provide an implementation of an inherited
abstract method, and
▶ No instances can be created from an abstract class.
In other words, no objects of abstract super classes can be
instantiated (created).
Abstract Classes
An abstract class does have a constructor that is invoked by its
subclasses, but it cannot be invoked directly.
A final class cannot contain any abstract methods
A class may be declared abstract even if it has no abstract
methods – this prevents instantiation.
Abstract class declaration
Abstract method
declaration
Abstract Classes
Although abstract classes cannot be used to instantiate objects,
they can be used to create object references.
Thus, it must be possible to create a reference to an abstract class
so that it can be used to point to a subclass object.
Abstract Classes – Example
Cont.
Abstract Classes – Example cont.
As you can see, we get error because abstract class cannot instantiate objects
Abstract Classes –
Example cont.
How to instantiated the
abstract class?
Answer: by Inheriting the
Abstract Class
We can inherit the properties
of Employee class just like
concrete class as shown here
Abstract Classes – Example cont.
Instantiate new object using Salary class instead of Employee
Result:
Abstract Classes – Example cont.
Abstract Methods
An abstract method is a method with the keyword abstract,
and it ends with a semicolon instead of a method
body(implementation).
For an abstract method , no implementation is required - only
the signature of the method is defined
Example:
Its implementation is provided by the subclasses.
private methods and static methods may not be declared
abstract.
Abstract Methods cont.
A class that contains abstract method(s) must be defined
abstract.
A non-abstract child class inherits the abstract method
and must define a non-abstract method that matches the
abstract method.
Abstract
Method :
Example
Abstract class :
Shape
(superclass / parent class )
Abstract
Method :
Example cont.
Child class :
Rectangle
Abstract
Method :
Example cont.
Child class : Circle
Abstract
Method :
Example cont.
Main class:
TestShape
Abstract
Method :
Example cont.
Main class :
TestShape cont.
Abstract Method : Main class : TestShape
Example cont. cont.
Content
Polymorphism concept
Abstract classes and methods
Method overriding
Array of super classes
Concrete sub classes and methods
Polymorphism vs Inheritance
In polymorphism, we will declare a method in superclass (either
concrete or abstract). Then, the subclass will override that method
with their own implementation. We call the method/function as
polymorphic function.
Those subclass objects will then be stored in a superclass array type.
When we want to call that method, we don’t have to convert the
type to subclass type. Java will decide which method to be run
based on the object type automatically.
Method Overriding Example
public class Order{
… Order
public double calcPrice() { -name
return price; -orderId
} -price
…
+calcPrice()
}
public class Postage extends
public class COD extends Order{ Order {
… …
public double calcPrice() { COD Postage public double calcPrice() {
return price + 1; return price + 5;
} }
+calcPrice() +calcPrice()
… …
+getCodTime() +getAddress()
} }
Method Overriding Example cont.
Order od[] = new Order[5];
od[0] = new COD(“ali”, “76”, 5, “10am”);
System.out.println(od[0].calcPrice()); //valid, will return 6
od[1] = new Postage(“abu”, “45”, 5, “No 32, Kampung Dusun”);
System.out.println(od[1].calcPrice()); //valid, will return 10
Content
Polymorphism concept
Abstract classes and methods
Method overriding
Array of super classes
Concrete sub classes and methods
Array of Superclasses
Array of superclass use to hold objects of different subclasses
Array of Superclass: Example
Array of Superclass: Example cont.
• If roster[i] refers to a GraduateStudent, then the computeCourseGrade
method of the GraduateStudent class is executed.
• If roster[i] refers to an UndergraduateStudent, then the computeCourseGrade
method of the UndergraduateStudent class is executed.
• For the above statements to be valid, method computeCourseGrade() must
exist in the superclass (class Student).
The instance of Operator
To determine the actual
class of an object
The instance of operator
is called the type
comparison operator
This operator evaluates
to true or false Result
Casting
Convert object that we store
in superclass data type into
the original class data type
We want to call
method/processor from the
original class which does not
exist in superclass.
Casting cont.
As you can see, even though a[0] contains object
of Frog, we can’t call the function jump() because
the object is currently stored in Animal data type.
Same with a[1]. Sleep() is valid because it is
declared in Animal, so it is guaranteed that all
classes that inherit from it will contain the method.
Casting cont.
To call the specific method in subclass when the
subclass object is stored in superclass data type, we
need to cast the object into subclass data type
Content
Polymorphism concept
Abstract classes and methods
Method overriding
Array of super classes
Concrete sub classes and methods
Concrete Class & Method
Concrete class is a class that can be instantiated.
Concrete class has to provide implementation of abstract methods.
Concrete class has to implement all abstract methods of the
abstract class in order to be used for instantiation.
Concrete class uses extends keyword
A derived class (subclass) that implements all the missing
functionality is called a concrete class.
Concrete Class & Method Example
Methods
implemented
by
Content
Polymorphism concept
Abstract classes and methods
Method overriding
Array of super classes
Concrete sub classes and methods
Chapter 6:
Polymorphism
CSC435: OBJECT ORIENTED PROGRAMMING
Prepared by Siti Norsyuhada Zainudin
Ammended from Bakri & Khairunnisa