What is Polymorphism in Java

What is Polymorphism in Java

Polymorphism in Java is the task that performs a single action in different ways.

So, languages that do not support polymorphism are not ‘Object-Oriented Languages’, but ‘Object-Based Languages’. Ada, for instance, is one such language. Since Java supports polymorphism, it is an Object Oriented Language.

Polymorphism in Java is the ability of a single method or class to take on multiple forms. It allows objects of different types to be treated as objects of a common type, providing a way to achieve flexibility and abstraction in programming. Polymorphism is achieved through two mechanisms in Java: method overloading and method overriding.

1. Method Overloading:

  • Method overloading is a form of compile-time (static) polymorphism, where multiple methods with the same name are defined within a class, but they differ in their parameter types, number of parameters, or both.
  • The compiler determines which method to invoke based on the method signature during compile-time.

Example
public class MathOperations {
    // Method to add two integers
    public int add(int a, int b) {
        return a + b;
    }

    // Method to add three integers
    public int add(int a, int b, int c) {
        return a + b + c;
    }

    // Method to concatenate two strings
    public String add(String str1, String str2) {
        return str1 + str2;
    }

    public static void main(String[] args) {
        MathOperations math = new MathOperations();

        // Method overloading based on parameters
        System.out.println(math.add(2, 3));
        System.out.println(math.add(2, 3, 4));
        System.out.println(math.add("Hello", "World"));
    }
}

2. Method Overriding:

  • Method overriding is a form of run-time (dynamic) polymorphism, where a subclass provides a specific implementation for a method that is already defined in its superclass.
  • The decision of which method to call is made at runtime based on the actual type of the object.

<strong>What is Polymorphism in Java</strong>

Example
// Parent class
class Animal {
    void makeSound() {
        System.out.println("Animal makes a sound");
    }
}

// Subclass overriding the makeSound method
class Dog extends Animal {
    @Override
    void makeSound() {
        System.out.println("Dog barks");
    }
}

// Subclass overriding the makeSound method
class Cat extends Animal {
    @Override
    void makeSound() {
        System.out.println("Cat meows");
    }
}

public class PolymorphismExample {
    public static void main(String[] args) {
        Animal myAnimal;

        // Polymorphic behavior based on the actual type of the object
        myAnimal = new Dog();
        myAnimal.makeSound(); // Calls the overridden method in Dog

        myAnimal = new Cat();
        myAnimal.makeSound(); // Calls the overridden method in Cat
    }
}

In this example, the makeSound method is overridden in the Dog and Cat subclasses, providing a different implementation for each. At runtime, the actual type of the object determines which version of the method is called.

Polymorphism allows for more flexible and extensible code by enabling code to work with objects of multiple types in a unified manner. It enhances the readability, maintainability, and scalability of the code.

Leave a Reply

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