Chapter 6 Polymorphism
Chapter 6 Polymorphism
Prepared by Daniel K
Chapter Six
Polymorphism
Introduction
Polymorphism is the ability of an object to take on many forms.
Polymorphism in Java is a concept by which we can perform a single action in different ways.
Polymorphism is derived from 2 Greek words: poly and morphs.
o The word "poly" means many and "morphs" means forms.
o So polymorphism means many forms.
Real-life Illustration Polymorphism:
o A person at the same time can have different characteristics. Like a man at the same
time is a father, a husband, and an employee. So the same person possesses
different behavior in different situations. This is called polymorphism.
What is Polymorphism in Java?
Polymorphism is considered one of the important features of OOP
Polymorphism allows us to perform a single action in different ways.
Polymorphism allows you to define one interface and have multiple implementations.
Types of Java polymorphism
In Java polymorphism is mainly divided into two types:
o Compile-time Polymorphism
o Runtime Polymorphism
Compile-Time Polymorphism
It is also known as static polymorphism.
This type of polymorphism is achieved by function overloading or operator overloading.
o Note: But Java doesn’t support the Operator Overloading.
Object oriented programming using java
Prepared by Daniel K
Method Overloading
o When there are multiple functions with the same name but different parameters then these
functions are said to be overloaded. Functions can be overloaded by changes in the number
of arguments or/and a change in the type of arguments.
Example for Java program for Method Overloading
// By using Different Types of Arguments // Class 2
// Class 1 // Main class
// Helper class
class GFG
class Helper
{
{
// Main driver method
// Method with 2 integer parameters
public static void main(String[] args)
static int Multiply(int a, int b)
{
{
// Calling method by passing
// Returns product of integer numbers
// input as in arguments
return a * b;
System.out.println(Helper.Multiply(2, 4));
}
System.out.println(Helper.Multiply(5.5, 6.3));
// Method 2
}
// With same name but with 2 double parameters
}
static double Multiply(double a, double b)
{
// Returns product of double numbers
return a * b;
}
}
Object oriented programming using java
Prepared by Daniel K
Example 2
// Java program for Method Overloading // Return product
// Class 1 }
// Helper class }
// Return product {
} // input as in arguments
{ }
Output
42
Object oriented programming using java
Prepared by Daniel K
Example
class A{}
class B extends A{}
A a=new B();//upcasting
Object oriented programming using java
Prepared by Daniel K
Example 1 on runtime polymorphism
// Java Program for Method Overriding {
// Class 1 // Print statement
// Helper class System.out.println("subclass2");
class Parent { }
// Method of parent class }
void Print() // Class 4
{ // Main class
// Print statement class GFG {
System.out.println("parent class"); // Main driver method
} public static void main(String[] args)
} {
// Class 2 // Creating object of class 1
// Helper class Parent a;
class subclass1 extends Parent { // Now we will be calling print
// Method methods
void Print() { // inside main() method
System.out.println("subclass1"); } a = new subclass1();
} a.Print();
// Class 3 a = new subclass2();
// Helper class a.Print();
class subclass2 extends Parent { } Output
// Method }
subclass1
void Print()
subclass2
child class is called. This is because the method in the parent class is overridden by the
child class. Since the method is overridden, this method has more priority than the parent
method inside the child class. So, the body inside the child class is executed.
Object oriented programming using java
Prepared by Daniel K
Example 2 on runtime polymorphism
In this example, we are creating two classes Bike and Splendor. Splendor class extends
Bike class and overrides its run() method. We are calling the run method by the reference
variable of Parent class. Since it refers to the subclass object and subclass method
overrides the Parent class method, the subclass method is invoked at runtime.
Since method invocation is determined by the JVM not compiler, it is known as runtime
polymorphism.
class Bike{
void run(){System.out.println("running");}
}
Output
class Splendor extends Bike{
void run(){System.out.println("running safely with 60km");} running safely with 60km.
eating meat...
Subtype of Run-time Polymorphism
Virtual functions
It allows an object of a derived class to behave as if it were an object of the base class.
The derived class can override the virtual function of the base class to provide its own
implementation. The function call is resolved at runtime, depending on the actual type of
the object.
Diagram –
Object oriented programming using java
Prepared by Daniel K
Polymorphism in Java is a concept that allows objects of different classes to be treated as
objects of a common class. It enables objects to behave differently based on their specific
class type.
of a common class.
Improves readability and maintainability of code by reducing the amount of code that
Supports dynamic binding, enabling the correct method to be called at runtime, based on
Enables objects to be treated as a single type, making it easier to write generic code that
Can make it more difficult to understand the behavior of an object, especially if the code
is complex.
This may lead to performance issues, as polymorphic behavior may require additional
computations at runtime.