Java Polymorphism

Java Polymorphism

What is Polymorphism?

In the Article, we will Discuss about Polymorphism in java.
Polymorphism is an essential feature of Java and allows for more flexible and extensible code. By utilizing polymorphism, a single method or class can be used in a variety of different contexts and with different types of objects.

Java Polymorphism

Polymorphism is one of the core concepts in object-oriented programming (OOP) and is supported in Java. Polymorphism refers to the ability of an object to take on many forms or to have multiple behaviors. In Java, there are two types of polymorphism: static (compile-time) polymorphism and dynamic (runtime) polymorphism.

Static polymorphism is achieved through method overloading, where multiple methods with the same name but different parameters are defined within the same class. The compiler determines which method to call based on the number, type, and order of arguments passed at compile-time.

Dynamic polymorphism, on the other hand, is achieved through method overriding, where a subclass provides its own implementation of a method that is already defined in its parent class. The method to be executed is determined at runtime based on the actual type of the object that the method is called on.

Types of Polymorphism in Java:

Run
public class Calculator {
    
    public int add(int a, int b) {
        return a + b;
    }

    public double add(double a, double b) {
        return a + b;
    }

    public int add(int a, int b, int c) {
        return a + b + c;
    }
    
    public static void main(String[] args) {
        Calculator calculator = new Calculator();
        System.out.println("Addition of two integers: " + calculator.add(2, 3));
        System.out.println("Addition of two doubles: " + calculator.add(2.5, 3.5));
        System.out.println("Addition of three integers: " + calculator.add(2, 3, 4));
    }
}

Explanation:

In the above program, the Calculator class has three add methods with the same name but different parameter lists. The first method takes two integer parameters, the second method takes two double parameters, and the third method takes three integer parameters.

In the main method, we create an instance of the Calculator class and call the add method with different parameter lists. The compiler determines which method to call based on the number and types of arguments passed to the method.

When we call calculator.add(2, 3), the first add method with two integer parameters is called and returns the sum of 2 and 3. When we call calculator.add(2.5, 3.5), the second add method with two double parameters is called and returns the sum of 2.5 and 3.5. Finally, when we call calculator.add(2, 3, 4), the third add method with three integer parameters is called and returns the sum of 2, 3, and 4.

Run
class Animal {
    public void makeSound() {
        System.out.println("Animal is making a sound");
    }
}

class Cat extends Animal {
    public void makeSound() {
        System.out.println("Meow");
    }
}

class Dog extends Animal {
    public void makeSound() {
        System.out.println("Woof");
    }
}

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

        animal1.makeSound(); // Output: Meow
        animal2.makeSound(); // Output: Woof
    }
}

Explanation:

In the above program, we have an Animal class with a makeSound method that prints “Animal is making a sound” to the console. We also have two subclasses, Cat and Dog, that extend the Animal class and override the makeSound method with their own implementations.
In the main method, we create two Animal objects, animal1 and animal2. We assign an instance of the Cat class to animal1 and an instance of the Dog class to animal2.
When we call animal1.makeSound(), the makeSound method of the Cat class is called and prints “Meow” to the console. When we call animal2.makeSound(), the makeSound method of the Dog class is called and prints “Woof” to the console. The method to be called is determined at runtime based on the actual object that is invoking the method, which is animal1 and animal2 respectively. This is an example of Runtime Polymorphism or Method Overriding in Java.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription