Java Polymorphism

The concept of polymorphism in Java allows us to carry out a single action in various ways. Greek words poly and morphs are the roots of the word polymorphism. Poly means numerous, and morphs means forms. Polymorphism entails diversity of forms.

Compile-time polymorphism and runtime polymorphism are the two types of polymorphism used in Java. By using method overloading and method overriding, we can implement polymorphism in Java.

1. Compile Time Polymorphism

Java also refers to this kind of polymorphism as static polymorphism or static method dispatch. Method overloading can be used to achieve it. An overloaded method is resolved in this process at compile time rather than at runtime.

Overloading of methods

Take a look at a class where several methods share the same name. The compiler won’t be able to distinguish between every method easily.

To solve this issue, we either pass the method a different number of arguments or different kinds of arguments. We accomplish method overloading in this way.

In other words, a class may contain multiple methods with the same name, each of which may be distinguished by either skipping a particular type of parameter or a particular number of parameters.

package com.Company;

// Example of Method Overloading in Java by GeektoCode
class CompileTime {
    // perimeter method with a single argument
    static int perimeter(int a) {
        return 4 * a;
    }
    // perimeter method with two arguments (overloading)
    static int perimeter(int l, int b) {
        return 2 * (l + b);
    }
}
class GeektoCode{
    public static void main(String[] args) {
        CompileTime obj = new CompileTime();
        // calling perimeter method by passing a single argument
        System.out.println("Side of square : 4\nPerimeter of square will be : " + obj.perimeter(4) + "\n");
        // calling perimeter method by passing two arguments
        System.out.println("Sides of rectangle are : 10, 13\nPerimeter of rectangle will be : " + obj.perimeter(10, 13));
    }
}

OUTPUT:-

Side of square: 4
Perimeter of square will be: 16
Sides of rectangle are: 10, 13
Perimeter of rectangle will be: 46

Explanation

The CompileTime class in the aforementioned example has two functions with the same name, but the first function only accepts one argument while the second function accepts two arguments.

Here, the perimeter of a square and a rectangle can be calculated using the perimeter() function. Compile-time polymorphism is accomplished and two functions with the same name are distinguished in this way.

When we invoke the perimeter method for the first time, we pass it a single integer, which causes the first method to be invoked. When we invoke it again, however, we pass it two integers, which causes the second method to be invoked.

2. Run Time Polymorphism

Dynamic method dispatch is another name for runtime polymorphism. The overridden method is resolved at runtime as opposed to being resolved at compile time.

Here, a method from a child class that has been overridden is called using its parent’s reference. The method is then invoked in accordance with the kind of object. The object type and associated method are determined by JVM at runtime.

In Java, runtime polymorphism happens when there are two or more classes that are related to one another through inheritance. We must create a “IS-A” relationship between classes and override a method in order to achieve runtime polymorphism.

Method Overriding

It’s referred to as method overriding when a child class inherits a method from its parent class.

It is referred to as method overriding if the derived class has a particular implementation of a method that has been declared in its parent class.

Java method overriding guidelines

  • There must be class-level inheritance.
  • The method must be the same for both classes (name of the class, number, and type of arguments must be the same).
package com.Company;

// Example of Method Overriding in Java by GeektoCode
class Animal{

    void place(){
        System.out.println("Animals live on earth.");
    }
}

class Dog extends Animal{

    void place(){
        System.out.println("Dog lives in kennel.");
    }
}

class Horse extends Animal{

    void place(){
        System.out.println("Horse lives in stable.");
    }
}

class Rabbit extends Animal{

    void place(){
        System.out.println("Rabbit lives in burrow.");
    }
}
class GeektoCode{
    public static void main(String[] args) {
        // creating object of Animal class
        Animal A = new Animal();
        A.place();
        A = new Dog();
        A.place();
        A = new Horse();
        A.place();
        A = new Rabbit();
        A.place();
    }
}

OUTPUT: –

Animals live on earth.
Dog lives in kennel.
Horse lives in stable.
Rabbit lives in burrow.

Explanation

The parent class in the example above is Animal, and the derived classes Dog, Horse, and Rabbit are where the place() method is overridden.

A new instance of the Animal class has been created. Every child class’s method is called when an object of that class is created. Since the child class overrides the parent class method.

Leave a Reply

Your email address will not be published. Required fields are marked *