ITC 1233 - OBJECT ORIENTED
PROGRAMMING
OBJECT ORIENTED CONCEPT 3: POLYMORPHISM
Ms.Nirasha Kulasooriya,
Lecturer, Dept. of ICT, FOT,USJ.
HAVE YOU HEARD ABOUT POLYMORPHISM?
WHAT IS MEANT BY POLYMORPHISM?
SO, WHAT WOULD BE THE MAIN
IMPORTANCE OF USING POLYMORPHISM?
DERIVING…
Poly + Morphism
Many + Forms
POLYMORPHISM
• The general meaning of polymorphism is “many
forms”.
• Polymorphism enables you to “program in the
general” rather than “program in the specific”.
POLYMORPHISM
Mainly there are two types of polymorphism in Java:
• Compile time polymorphism
• Method overloading
• Runtime polymorphism
• Method overriding
POLYMORPHISM
METHOD OVERLOADING
• Method overloading is a feature that allows a class
to have more than one method having the same
name, if their parameter lists are different.
• A method is overloaded when the class provides
multiple implementations of the same method with
the same name, but with different parameters.
METHOD OVERLOADING
Java mainly allows for three ways of method overloading:
1. Different number of parameters
2. Parameters with different data types
3. Sequence of data types of parameters
Overloaded methods can have different return types, if
the methods have different parameter lists.
Student.java
EXAMPLE: METHOD
OVERLOADING
Test.java
EXAMPLE:
METHOD
OVERLOADING
CONSTRUCTOR OVERLOADING
• Similar to method overloading, constructors can
also be overloaded. This is called constructor
overloading.
• Constructor overloading is a feature that allows a
class to have more than one constructor, if their
parameter lists are different.
CONSTRUCTOR OVERLOADING
A constructor is overloaded when the class provides
implementations of the same constructor, but with
different parameters. Java mainly allows for three ways
of constructor overloading:
1. Different number of parameters
2. Parameters with different data types
3. Sequence of data types of parameters
Student.java
EXAMPLE:
CONSTRUCTOR
OVERLOADING
Test.java
EXAMPLE:
CONSTRUCTOR
OVERLOADING
ACTIVITY 01
• Choose and discuss a real world scenario where you can
apply method overloading for the functioning.
• Explain 02 scenario specific advantages regarding the
scenario chosen.
CAN YOU REMEMBER WHAT IS HIERARCHICAL
INHERITANCE?
HIERARCHICAL INHERITANCE
HIERARCHICAL INHERITANCE
JUST THINK CAN WE ACHIEVE THIS?
USING POLYMORPHISM…
One of the most common uses of polymorphism can be
observed when the superclass reference can be used to
refer to a subclass object.
METHOD OVERRIDING
• Method overriding allows a subclass to provide its own specific
implementation of a method that is provided by its superclass.
• A method in the superclass is overridden when the subclass
provides an implementation of the same method with the same
name, same parameters and same return type. Then, it can be
said that, the method in the subclass overrides the method in
the super-class.
EXAMPLE: INHERITING METHODS FROM THE
SUPERCLASS
Vehicle.java
EXAMPLE: INHERITING METHODS FROM THE SUPERCLASS
Car.java
EXAMPLE: INHERITING METHODS FROM THE SUPERCLASS
Test.java
EXAMPLE: METHOD OVERRIDING
Vehicle.java
EXAMPLE: METHOD OVERRIDING
Car.java
EXAMPLE: METHOD OVERRIDING
Test.java
METHOD OVERRIDING: ACCESS MODIFIERS
• When a subclass method overrides a superclass method, the
access modifier of the sub-class method should be equal or
less restrictive than the access modifier of the superclass
method.
• If the access modifier of a superclass method is private, it
cannot be overridden because it will not be inherited.
METHOD OVERRIDING: ACCESS MODIFIERS
ACTIVITY 02
Employee Hierarchy:
• Define an Employee class with attributes like name and salary,
and a method calculateBonus().
• Create subclasses like Manager, Developer, and SalesPerson that
extend the Employee class.
• Override the calculateBonus() method in each subclass to apply
specific bonus calculations.
• Illustrate polymorphism by creating an array of Employee objects,
including instances of subclasses, and calculate bonuses.
ANY QUESTIONS?