Java – Object Oriented Programming Concepts – Polymorphism


What is polymorphism ?

A certain form is modified in to several other different forms based on condition.

What is polymorphism in java ?

It is defined as the state of an object to taking many other forms.

Uses of polymorphism :

Polymorphism allows objects having different internal forms to share the same external interface.

The main purpose is to achieve multiple inheritance easier since objects can behave in multiple forms.

Polymorphism allows you define one interface and have multiple implementations.

Types of polymorphism :

1. Static polymorphism.

2. Dynamic polymorphism.

1. Static polymorphism :

Static Polymorphism is a compile time binding polymorphism.

This binding occurs in the compile time.

Method overloading can be achieved in static polymorphism.


Method overloading is done by declaring same method with different parameters.

1. Method Overloading with different parameters.

Example 1 :

public class Eg1{
public static void main(String args[])
{
Eg2 obj = new Eg2();
obj.print1(10);
obj.print1(1,"Abdul Kalam");
}
}

class Eg2{
public void print1(int a)
{
        System.out.println("Inside Method print1, Printing The value of a = "+a);
}
public void print1(int a,String Name)
{
        System.out.println("Inside Same Method print1, Printing The value of a = "+a);
        System.out.println("Inside Same Method print1, Printing The value of Name ="+Name);
}
}

Output :

Inside Method print1, Printing The value of a = 10

Inside Same Method print1, Printing The value of a = 1

Inside Same Method print1, Printing The value of Name =Abdul Kalam

2. Method Overloading with different data-types.

Example 1 :

public class Eg1{
public static void main(String args[])
{
Eg2 obj = new Eg2();
obj.print1(10);
obj.print1("Abdul Kalam");
}
}

class Eg2{
public void print1(int a)
{
System.out.println("Inside Method print1 with int param, Printing The value of a = "+a);
}
public void print1(String a)
{
System.out.println("Inside Same Method print1 with String param, Printing The value of a ="+a);
}
}

Output :

Inside Method print1 with int param, Printing The value of a = 10

Inside Same Method print1 with String param, Printing The value of a =Abdul Kalam

3. Method Overloading with type promotions

Data type of smaller size is promoted to the data type of bigger size than this is called type promotion.

1. Byte can be promoted to short, int and long

2. short can be promoted to int and long

3. int can be promoted to long, float and double

4. float can be promoted to double

5. long can be promoted to float and double

Suppose if i am passing float value as a second argument but it got promoted to the type double, because there wasn’t any method having arg list as (int, float)

Example 1 :

class Eg1{
   void print1(int a, double b){
        System.out.println("Calling Method 1");
   }
   void print1(int a, double b, double c){
        System.out.println("Calling Method 2");
   }
   public static void main(String args[]){
        Eg1 obj = new Eg1();
        obj.print1(10, 11.7f);
   }
}

Output :

Calling Method 1

2. Dynamic Polymorphism 

Dynamic polymorphism in Java is achieved by method overriding.

1. Method overriding :

When Parent class reference refers to the parent class object then in this case overridden method (the method of parent class) is called.

When parent class reference refers to the child class object then the overriding method (method of child class) is called.

This is called dynamic method dispatch and runtime polymorphism.

Example 1 :

class Eg2 extends MainClass{
   public void show(){
System.out.println("Printing info from the method of Child class");
   }
   public static void main( String args[]) {
MainClass obj = new MainClass();
obj.show();
MainClass obj2 = new Eg2();
obj2.show();
   }
}
class MainClass{
   public void show()
   {
        System.out.println("Printing info from the method of parent class");
   }      
}

Output :

Printing info from the method of parent class


Printing info from the method of Child class

2. Method overriding by super keyword :

Methods can be overridden by using super keyword.

Example 1 :

public class Eg3 extends MainClass{
        public void show()
        {
                super.show();
                System.out.println("Printing from sub-class");
        }
        public static void main(String args[])
        {
                Eg3 obj = new Eg3();
                obj.show();
        }
}
class MainClass{
        public void show()
        {
                System.out.println("Printing from the main class...");
        }
}

Output :

Printing from the main class…

Printing from sub-class 

Rules to be followed to achieve Dynamic polymorphism / Method overriding :

1. Argument list of the methods in parent and child class should be the same.
2. Access modifier of the methods in parent and child class should be the same.
3. private, static and final methods cannot be overridden because it is considered as local to the class.
4. Dynamic binding of overriding methods will occur at the runtime.
5. abstract class will override all the abstract methods.
6. static methods can be redeclared in the sub-class.