Polymorphism in Java

Polymorphism is one of the fundamental concepts of object-oriented programming and is a feature of Java. It refers to the ability of an object to take on multiple forms or types. Polymorphism allows objects of different classes to be treated as if they are of the same type. There are two types of polymorphism in Java: 
  1. Compile-time polymorphism: also known as method overloading, it allows multiple methods with the same name to be defined in a class, but with different parameter lists. The correct method to be called is determined at compile time-based on the number, type, and order of the arguments passed to it.
  2. Runtime polymorphism: also known as method overriding, it allows a subclass to provide its own implementation of a method defined in its superclass. The method in the subclass must have the same signature (name, return type, and parameter list) as the method in the superclass. The correct method to be called is determined at runtime based on the actual type of the object on which the method is called.

Polymorphism enables the creation of generic code that can work with objects of different classes, as long as they have a common superclass or interface. It is a powerful feature that promotes code reuse and makes it easier to write modular, extensible, and maintainable code. 
 
Compile-time Polymorphism (Method Overloading):
public class Calculator {
    public int add(int x, int y) {
        return x + y;
    }

    public double add(double x, double y) {
        return x + y;
    }
}

public class Main {
    public static void main(String[] args) {
        Calculator calculator = new Calculator();

        int sum1 = calculator.add(2, 3); // calls the int version of add()
        double sum2 = calculator.add(2.5, 3.5); // calls the double version of add()
    }
}
In this example, we have a Calculator class with two methods named add(). One method takes two int parameters and returns an int, while the other takes two double parameters and returns a double. The compiler determines which version of the method to call based on the types of arguments provided. This is an example of compile-time polymorphism because the decision of which method to call is made at compile-time. 
 
Run-time Polymorphism (Method Overriding):
public class Animal {
    public void speak() {
        System.out.println("The animal makes a sound");
    }
}

public class Dog extends Animal {
    public void speak() {
        System.out.println("The dog barks");
    }
}

public class Cat extends Animal {
    public void speak() {
        System.out.println("The cat meows");
    }
}

public class Main {
    public static void main(String[] args) {
        Animal animal1 = new Dog();
        Animal animal2 = new Cat();

        animal1.speak(); // outputs "The dog barks"
        animal2.speak(); // outputs "The cat meows"
    }
}
In this example, we have an `Animal` class with a method named `speak()`, which outputs a generic message that the animal makes a sound. We then define two subclasses, `Dog` and `Cat`, that override the `speak()` method with their own specific message. In the `Main` class, we create two `Animal` objects, one of type `Dog` and one of type `Cat`. When we call the `speak()` method on each object, the version of the method that's called depends on the type of the object. This is an example of run-time polymorphism because the decision of which method to call is made at run-time based on the type of the object. 
 
Both types of polymorphism are important in Java and are widely used to make code more flexible and reusable.
Next Post Previous Post
No Comment
Add Comment
comment url