Polymorphism in Java

  • Polymorphism is derived from two Greek words: poly and morph
  • Poly means many and morph means form.
  • Polymorphism is the ability to present same interface in different forms.
  • So, polymorphism describes a pattern in which classes have different functionalities while sharing a common interface.

Types of Polymorphism

1. Runtime polymorphism
  • Compiler cannot determine the method at compile time.
  • Method overriding is the perfect example of runtime polymorphism.
  • It is also called as dynamic polymorphism.

Example: Program for runtime polymorphism

class A
{
    A()
    {
        System.out.println("Constructor of A ");
    }
    public void show()
    {
        System.out.println ("method of class A");
    }
}
class B extends A
{
    B()
    {
         System.out.println("Constructor of B");
    }
    public void show()
    {
        System.out.println ("method of class B");
    }
}
class C extends B
{
   C()
   {
        System.out.println("Constructor of C");
   }
   public void show()
   {
       System.out.println("method of class C");
   }
}
public class D
{
   public static void main (String args [])
   {
       A obj1 = new A();
       A obj2 = new B();
       B obj3 = new C();
       obj1.show();
       obj2.show();
       obj3.show();
   }
}


Output:
Constructor of A
Constructor of A
Constructor of B
Constructor of A
Constructor of B
Constructor of C
method of class A
method of class B
method of class C

2. Compile Time Polymorphism
  • Method overloading is nothing but compile time polymorphism in Java.
  • It is checked by the compiler at compile time.
  • Compile time polymorphism is also known as static polymorphism.

Example: Program for compile time polymorphism

class Base
{
    void sum (int a, int b)
    {
        System.out.println ("The sum of integer: "+(a+b));
    }
    void sum (double a, double b)
   {
        System.out.println ("The sum of double: "+(a+b));
    }
    void sum (String a, String b)
   {
        System.out.println ("The sum of String: "+(a+b));
    }
}
public class Derive
{
    public static void main(String args[])
   {
        Base base = new Base();
        base.sum(45,35);
        base.sum(61.3,45.7);
        base.sum("Java", " Tutorial");
    }
}


Output:
The sum of integer: 80
The sum of double: 107.0
The sum of String: Java Tutorial

Method Overloading

  • If a class has more than one method with the same name but different parameter, it is known as method overloading.
  • Code readability of the program can be increased by method overloading.

Example: Program for method overloading

class OverLoadDemo
{
    void sum (int a, int b)
    {
        System.out.println ("The sum of integer: "+(a+b));
    }
    void sum (double a, double b)
    {
        System.out.println ("The sum of double: "+(a+b));
    }
    void sum (int a, double b)
    {
        System.out.println ("The sum of int and double: "+(a+b));
    }
    void sum (String a, String b)
    {
        System.out.println ("The sum of String: "+(a+b));
    }
    public static void main(String args[])
    {
        OverLoadDemo over = new OverLoadDemo();
        over.sum(20,35);
        over.sum(21.3,18.7);
        over.sum(17, 24.6);
        over.sum("CareerRide", " Info");
    }
}


Output:
The sum of integer: 55
The sum of double: 40.0
The sum of int and double: 41.6
The sum of String: CareerRide Info

Method Overriding

In method overriding, both base and derived class have the same method signature. It means method name and arguments are same in both classes.

Rules for method overriding
  • Method must be written in child class, not in same class.
  • Method name and argument must be same.
  • A final method cannot be overridden.
  • Method should be inherited means IS-A relationship.
  • Return type should be same.

Example: Program for method overriding

class Base
{
   public void show(int a)
   {
      System.out.println("show method of Base class");
   }
   public void display()
  {
      System.out.println("display method of Base class");
   }    
}
public class Derive extends Base
{
   public void show(int a)
   {
      System.out.println("show method of Derive class");
   }
   public void xyz()
   {
      System.out.println("xyz() method of derive class");
   }
   public static void main( String args[])
   {
      Base obj = new Derive();
      obj.show(2);
      obj.display();
   }
}


Output:
show method of Derive class
display method of Base class