Java Polymorphism

Java Polymorphism

Polymorphism in Java is made up of two words, “poly” meaning “many” and morphs meaning “forms”. It is one of the important features of an object-oriented programming language. It is defined as the ability of a message to be displayed in many different forms.

Real-life Illustration of Polymorphism:

A person in its life can take many forms depending upon the environment it is in. Like a woman can be a mother, a daughter, a grandmother, a housewife, or an employee. This is what we call polymorphism in real-life.

 

 

Types of Polymorphism in Java

In Java, polymorphism can be divided into two main types:

  • Compile-time Polymorphism
  • Runtime Polymorphism

 

Compile-time Polymorphism in Java

It is also known as static polymorphism. In Java, it can be achieved through function overloading only. Java does not allow operator overloading.

Runtime Polymorphism in Java

It is also known as dynamic polymorphism or dynamic method dispatch. It can be achieved through method overriding where calls to overridden methods are resolved by the interpreter at run-time.

 

 

Method Overloading in Java

Method overloading is the mechanism in which more than one method has the same name and call to those methods (overloaded methods) is resolved at compile-time on the basis of a number of parameters and types of parameters.

Example of Method Overloading:

//Java program with method overloading
public class MethodAdd {
    //performs addition on two integer types
    static int add(int a, int b){
        return (a+b);
    }

    //performs addition on two double types
    static double add(double a, double b){
        return (a+b);
    }

    //performs addition on three integer types
    static int add(int a, int b, int c){
        return (a+b+c);
    }

    public static void main(String[] args) {
        //method calling inside System.out.println()
        System.out.println("Addition of 5 and 6 is: "+add(5,6));
        System.out.println("Addition of 5 and 6 is: "+add(5,6,7));
        System.out.println("Addition of 5 and 6 is: "+add(5.9,6.5));
    }
}

Output:

Addition of 5 and 6 is: 11
Addition of 5 and 6 is: 18  
Addition of 5 and 6 is: 12.4

 

 

Method Overriding in Java

Method overloading is the mechanism in which more than one method has the same name, a number of parameters, and types of parameters, and call to those methods (overloaded methods) is resolved at run-time. It occurs when the child class has provided its own definition for the base class method that it has acquired during inheritance. The base class method is then said to be overridden. A reference variable of parent type is created and can be referred to as a subtype of the declared type to resolve method calls during run-time.

Example of Method Overriding:

//Java Program with method overriding
class ClassA{
    void display(){
        System.out.println("Display method of parent class.");
    }
}
class ClassB extends ClassA{
    void display(){
        System.out.println("Display method of child class.");
    }
}
public class MethodOverriding {
    public static void main(String[] args) {
        
        ClassA a = new ClassA();
        a.display();

        a=new ClassB();
        a.display();
    }


}

Output:

Display method of parent class.
Display method of child class.

 

Share this post

Leave a Reply

Your email address will not be published.