Java Polymorphism

Poly means many and morph means form. Polymorphism means the phenomenon of having many forms. Java Polymorphism refers to the phenomenon where many classes are related to each other through inheritance. Polymorphism occurs when different implementations of a function with same name exists in different classes. The function invoked depends on the type of object calling the function.

The real life example of polymorphism is water.  Water can be in solid state as ice, in liquid state as water and in gaseous state as water vapors or steam. The properties of water in theses three states is different but its composition is same. This is called polymorphism.

The example below demonstrates the concept of Java polymorphism: Here, class A is the superclass that has three subclasses B, C and D. Each subclass has its own member method having same name print_website(). The name of method is same in all classes but the code inside the method is different. Each print_website() method has a different implementation.

The compiler executes the method depending on the class of object.

class A {
   public void print_greetings()
    {
        System.out.print("Welcome to ");
    }
}

class B extends A {
    public void print_website() 
    { 
        System.out.println("TutorialsArt"); 
    }
   
   }

class C extends A {
    public void print_website()
    {
        System.out.println(" Google");
    }
}

class D extends A {
    public void print_website()
    {
        System.out.println(" Youtube");
    }
}	
   
public class Main {
    public static void main(String[] args)
    {
        B obj1 = new B();
        obj1.print_greetings();
        obj1.print_website();
 
        C obj2 = new C();
        obj2.print_greetings();
        obj2.print_website();
 
        D obj3 = new D();
        obj3.print_greetings();
        obj3.print_website();
    }
}

The object of class B executes the method of its own class and so do the objects of other class C and D.The output of the above code after compilation is : Java Polymorphism

Types of Java Polymorphism

There are two types of Java Polymorphism:

  • Compile time Polymorphism
  • Run time Polymorphism

Compile time Polymorphism

Compile time polymorphism is the type of Java Polymorphism that uses Method Overloading. Java does not support Operator Overloading.

Method Overloading

Two or more than two methods having same name but different parameters are overloaded methods. And this phenomenon is called Method Overloading. Multiple methods can achieve the phenomenon of overloading either by varying number of parameters, or varying type of parameters or both.

The example below shows method overloading by varying type of arguments. The class Product has two methods having same name Multiplication(). But the type of arguments that these methods take is different. One takes int type arguments and other takes double type arguments.

class Product {
  
    // Method with two int type parameters
    static int Multiplication(int x, int y)
    {
        return x * y;
    }
  
    // Method with two double type parameters
    static double Multiplication(double x, double y)
    {
        return x * y;
    }
}
  
public class Example {
    public static void main(String[] args)
    {
  
        System.out.println(Product.Multiplication(3, 2));
  
        System.out.println(Product.Multiplication(3.2, 4.6));
    }
}

The type of parameters entered decide which method to be executed. The output of the above code after compilation is:

Here is another example that shows method overloading by varying number of arguments:

class Product {
  
    // Method with two parameters
    static int Multiplication(int x, int y)
    {
        return x * y;
    }
  
    // Method with three parameters
    static int Multiplication(int x, int y, int z)
    {
        return x * y * z;
    }
}
  
public class Example {
    public static void main(String[] args)
    {
  
        System.out.println(Product.Multiplication(3, 2));
  
        System.out.println(Product.Multiplication(3, 6, 2));
    }
}

The number of parameters entered decide which method to be executed. The output of the above code after compilation is:

Run time Polymorphism

Run time polymorphism is the type of Java Polymorphism that uses the phenomenon of Method Overriding.

Method Overriding

Method overriding is a part of inheritance. It occurs when the method of subclass overrides the method definition of superclass. The subclass has its own definition for one of the member methods of superclass. The superclass has one virtual method that can be overridden. That method is called overridden.

The superclass A has a generic definition of the overridden method Display(). The subclasses B and C have their own definitions of the Display() method that override the method definition of superclass A. The below example shows the concept of method overriding:

class A {
  
    void Display()
    {
        System.out.println("Superclass A");
    }
}
  
class B extends A {
  
    void Display()
    {
        System.out.println("Subclass B");
    }
}
  
class C extends A {
  
    void Display()
    {
        System.out.println("Subclass C");
    }
}
  
class Main {
    public static void main(String[] args)
    {
  
        A a;
  
        a = new B();
        a.Display();
  
        a = new C();
        a.Display();
    }
}

The output of the above code after compilation is: