POLYMORPHISM IN JAVA
By: Diksha Tripathi
(M.Sc Computational Science & Applications with Specialization in Data Science)
Banaras Hindu University
AGENDA
What is Polymorphism?
Examples of Polymorphism
Types of Polymorphism
Essential conditions for achieving Polymorphism
Static Polymorphism
Q&A based on Static Polymorphism
Dynamic Polymorphism
Q&A based on Dynamic Polymorphism
Static v/s Dynamic Polymorphism
What is Polymorphism?
Polymorphism come from the two Greek words.
‘poly’ meaning many and ‘morphs” meaning forms
The word polymorphism means having many forms.
In JAVA, Polymorphism is the ability of an entity to take several forms.
Examples of Polymorphism
Pic Source: edureka
Types of Polymorphism
Static Polymorphism/Compile time Polymorphism/Static Binding/Early binding
Dynamic Polymorphism/Run time Polymorphism/Dynamic Binding/Late Binding
STATIC POLYMORPHISM DYNAMIC
POLYMORPHISM
• Two or more Methods of Same Name • Two or more methods of Same Name
• Within Same Class • Within Different Class
• Different Arguments • Same Arguments
No. of arguments or No. of arguments or
Sequence of arguments or Sequence of arguments or
Type of arguments Type of arguments
• Inheritance
Static Polymorphism
It is also known as compile time polymorphism. This type of polymorphism is achieved
by method overloading.
Method overloading: Method overloading is a feature that allows a class to include two or more
methods to have the same name but different parameter list.
Sample program for Static Polymorphism
Program
Output
Questions:
1. Can we achieve method overloading by changing the return type of method only?
2. Can we overload main() method?
3. Does method overloading allows Type Promotion?
Answer:
1. In Java, method overloading is not possible by changing the type of the method
only because of ambiguity.
2. Yes, we can overload main method also.
3. Yes, One type is automatically promoted to another implicitly if no matching Data
type is found. Below is the diagram:
Dynamic Polymorphism
It is also known as Run time Polymorphism. It is a process in which a function call to the
overridden method is resolved at Runtime. This type of polymorphism is achieved by
Method Overriding.
Method Overriding: Overriding is a feature that allows a subclass or child class to provide a
specific implementation of a method that is already provided by one of its super-classes or parent
classes.
Sample Program for Dynamic Polymorphism
Program:
Output:
Questions:
1. Do overriding methods must have same return type(or subtype)?
2. What is the relationship between Overriding and Access- Modifier?
3. Can we invoke overridden method from sub-class?
4. Can all methods overridden?
Ans 1: From Java 5.0 onwards it is possible to have different return type or a overriding method in child
class, but child’s return type should be sub-type of parent’s return type. It is known as Covariant return type.
Ans 2: The access modifier for an overriding method can allow more, but not less, access than the
overridden method.
For example, a protected instance method in the super class can be made public, but not private, in the
subclass. Doing so, will generate compile-time error.
Ans 3:Yes, we can call parent class method in overriding method by using super
keyword.
Ans 4: No, Final, Static and Private methods cannot be overridden.
Static v/s Dynamic Polymorphism
source: slideshare
THANK YOU