Polymorphism in Java with some example , Compile-time Polymorphism, Run-time polymorphism, Advantages of Polymorphism

 ### Polymorphism in Java


Polymorphism is a core concept in object-oriented programming that allows objects to be treated as instances of their parent class rather than their actual class. It enables a single interface to represent different underlying forms (data types). There are two types of polymorphism in Java:


1. **Compile-time polymorphism (Static polymorphism)**: Achieved through method overloading.

2. **Run-time polymorphism (Dynamic polymorphism)**: Achieved through method overriding.


#### 1. Compile-time Polymorphism (Method Overloading)


Method overloading occurs when a class has multiple methods with the same name but different parameters (different type, number, or both).


**Example:**


```java

class MathOperation {

    // Method to add two integers

    int add(int a, int b) {

        return a + b;

    }


    // Method to add three integers

    int add(int a, int b, int c) {

        return a + b + c;

    }


    // Method to add two double values

    double add(double a, double b) {

        return a + b;

    }

}


public class Main {

    public static void main(String[] args) {

        MathOperation math = new MathOperation();

        System.out.println(math.add(2, 3)); // Output: 5

        System.out.println(math.add(2, 3, 4)); // Output: 9

        System.out.println(math.add(2.5, 3.5)); // Output: 6.0

    }

}

```


In the above example, the `add` method is overloaded with different parameters.


#### 2. Run-time Polymorphism (Method Overriding)


Method overriding occurs when a subclass provides a specific implementation of a method that is already defined in its superclass. It is used to achieve run-time polymorphism.


**Example:**


```java

class Animal {

    // Method to be overridden

    void sound() {

        System.out.println("Animal makes a sound");

    }

}


class Dog extends Animal {

    // Overriding the sound method

    @Override

    void sound() {

        System.out.println("Dog barks");

    }

}


class Cat extends Animal {

    // Overriding the sound method

    @Override

    void sound() {

        System.out.println("Cat meows");

    }

}


public class Main {

    public static void main(String[] args) {

        Animal myAnimal; // Reference variable of type Animal

        

        myAnimal = new Dog(); // myAnimal refers to a Dog object

        myAnimal.sound(); // Output: Dog barks

        

        myAnimal = new Cat(); // myAnimal refers to a Cat object

        myAnimal.sound(); // Output: Cat meows

    }

}

```


In the above example, the `sound` method is overridden in the `Dog` and `Cat` classes. At runtime, the JVM determines the actual object type and calls the overridden method.


### Advantages of Polymorphism


1. **Code Reusability**: Allows the same code to handle different types and classes, reducing redundancy.

2. **Flexibility**: Makes the code more flexible and scalable, enabling the extension of classes and interfaces without changing existing code.

3. **Maintainability**: Enhances code maintainability by organizing related methods under a common interface or superclass.


### Conclusion


Polymorphism in Java, through method overloading and method overriding, is a powerful concept that promotes flexibility, reusability, and maintainability in code. It allows for writing more generic and extensible code by treating objects as instances of their parent class rather than their actual class.

Post a Comment

Previous Post Next Post