Polymorphism in Java


Polymorphism refers to "having many forms" or more than one form. In OOPs, it is the ability to create a variable, function or object which has many form. In the Java Programming Language, two types of Polymorphism are there:

1. Compile Time Polymorphism

Also known as Early Binding or Static Binding. In this case, compiler identifies that which polymorphic form is to be executed at compile time. Method overloading and Operator overloading are good example of Compile Time Polymorphism. The following written program demonstrates Compile Time Polymorphism.

Method Overloading
When a class contains methods of the same name but of different number of parameters, is called Method Overloading.

Code

public class MehodOverloading {

    public static void main(String[] args) {
        MyClass obj = new MyClass();
        int sum = obj.Add(4, 5);
        System.out.println(sum);
        sum = obj.Add(4, 5, 7);
        System.out.println(sum);
        sum = obj.Add(4, 5, 7, 9);
        System.out.println(sum);
    }
}

class MyClass {

    public int Add(int a, int b) {
        return a + b;
    }

    public int Add(int a, int b, int c) {
        return a + b + c;
    }


    public int Add(int a, int b, int c, int d) {
        return a + b + c + d;
    }
}

Output

9
16
25

2. Runtime Polymorphism

Also known as Late Binding or Virtual Method Invocation or Dynamic Binding. In Runtime Polymorphism, the compiler identifies which polymorphic form is to be executed at runtime but not in compile time, is called Runtime Polymorphism. Method overriding is a well known example of Runtime Polymorphism.

Method Overriding
Method Overriding takes place, if Inheritance exists and a super class and subclass have the method of same signature. Signature refers to access specifier, return type, name, parameters(number, type & order) & throws clause of the method. Then, the method of the subclass overrides the method of the superclass. As demonstrated in the following written program.

Code

public class MethodOverriding {

    public static void main(String[] args) {
        ClassB obj = new ClassB();
        obj.Display(10, 15);
    }
}

class ClassA {

    public void Display(int a, int b) {
        a += 5;
        b += 7;
        System.out.println("Sum is " + (a + b));
    }
}

class ClassB extends ClassA {

    public void Display(int a, int b) {
        System.out.println("Sum is " + (a + b));
    }
}

Output

Sum is 25

In this case, the method of ClassB overrides the method of ClassA. The method of ClassA is said to be Overridden. To call the overridden method, you need to use the super keyword as demonstrated in the following program.

Code

public class MethodOverriding {

    public static void main(String[] args) {
        ClassB obj = new ClassB();
        obj.Display(10, 15);
    }
}

class ClassA {

    public void Display(int a, int b) {
        a += 5;
        b += 7;
        System.out.println("Sum is " + (a + b));
    }
}

class ClassB extends ClassA {

    public void Display(int a, int b) {
        System.out.println("Sum is " + (a + b));
        super.Display(a, b);
    }
}

Output

Sum is 25
Sum is 37


In the above program, the "super" keyword is used to call the member of the super class. The "super" keyword is also used to call the constructor of the super class, demonstrated in the following program.

Using "super" keyword to call the constructor of super class

Code

public class SuperKeyword {

    public static void main(String[] args) {
        ClassC obj = new ClassC();
        obj.show();
    }
}

class ClassA {

    static int a;
    static int b;
 
    ClassA() {
        a = 7;
        b = 8;
    }
}

class ClassB extends ClassA {

    ClassB() {
        super();
    }
}

class ClassC extends ClassB {

    ClassC() {
        super();
    }
 
    public void show() {
        System.out.println("Sum is " + (a + b));
     }
}

Output

Sum is 15

Note: Call to super must be first statement in constructor.

Hidden Method

Consider the case of method overriding, the method of the superclass is the overridden method. In the case of Method hiding, the concept is the same but methods in both classes are static. Now, the method of the subclass hides the method of the superclass. So the method of the superclass is called a Hidden Method. To call the hidden method, you need to provide the 'class reference', instead of using the super keyword. For example:

Code

public class MethodOverriding {

    public static void main(String[] args) {
        ClassB obj = new ClassB();
        obj.Display(10, 15);
    }
}

class ClassA {

    public static void Display(int a, int b) {
        a += 5;
        b += 7;
        System.out.println("Sum is " + (a + b));
    }
}

class ClassB extends ClassA {

    public static void Display(int a, int b) {
        System.out.println("Sum is " + (a + b));
        ClassA.Display(a, b);
    }
}

Output

Sum is 25
Sum is 37

Up Next
    Ebook Download
    View all
    Learn
    View all