polymorphism in java

polymorphism in java

Polymorphism is a core concept in object-oriented programming (OOP) that refers to the ability of a variable, function, or object to take on multiple forms. In Java, polymorphism allows methods to do different things based on the object they are acting upon, even though they share the same name. This capability leads to more flexible and maintainable code. Polymorphism can be achieved through method overloading and method overriding.

polymorphism in java

Types of Polymorphism:

  • 1. Compile-time Polymorphism (Static Polymorphism):
    • Achieved through method overloading.
    • Method overloading allows multiple methods in the same class to have the same name but different parameters (different type or number of parameters).
  • 2. Runtime Polymorphism (Dynamic Polymorphism):
    • Achieved through method overriding.
    • Method overriding allows a subclass to provide a specific implementation of a method that is already defined in its superclass.

Compile-time Polymorphism (Method Overloading):

Method overloading is the ability to create multiple methods with the same name in the same class, but with different parameter lists.

Example
```java
class MathOperations {
    // Overloaded method with two integer parameters
    int add(int a, int b) {
        return a + b;
    }

    // Overloaded method with three integer parameters
    int add(int a, int b, int c) {
        return a + c + b;
    }

    // Overloaded method with two double parameters
    double add(double a, double b) {
        return a + b;
    }
}

public class Main {
    public static void main(String[] args) {
        MathOperations math = new MathOperations();
        
        // Calls the method with two integer parameters
        System.out.println(math.add(10, 20)); // Output: 30

        // Calls the method with three integer parameters
        System.out.println(math.add(10, 20, 30)); // Output: 60

        // Calls the method with two double parameters
        System.out.println(math.add(10.5, 20.5)); // Output: 31.0
    }
}
```

Runtime Polymorphism (Method Overriding):

Method overriding occurs when a subclass provides a specific implementation for a method that is already defined in its superclass. The method in the subclass must have the same name, return type, and parameters as the method in the superclass.

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

class Dog extends Animal {
    @Override
    void makeSound() {
        System.out.println("Dog barks");
    }
}

class Cat extends Animal {
    @Override
    void makeSound() {
        System.out.println("Cat meows");
    }
}

public class Main {
    public static void main(String[] args) {
        Animal myAnimal;
        
        myAnimal = new Dog();
        myAnimal.makeSound(); // Output: Dog barks

        myAnimal = new Cat();
        myAnimal.makeSound(); // Output: Cat meows
    }
}
```

Importance of Polymorphism:

  • 1. Code Reusability: Polymorphism allows methods to be written in a generic way, enabling code to be reused across different types without modification.
  • 2. Flexibility and Maintainability: It enhances flexibility and maintainability by allowing one interface to be used for a general class of actions. This means you can change parts of the system without affecting other parts.
  • 3. Extensibility: New classes can be added with little or no modification to existing code, as they can be treated polymorphically.
  • 4. Improved Code Organization: Polymorphism allows for the design of better organized and structured code, making it easier to understand and manage.

Example with Interface:

Polymorphism is often used with interfaces to define a common set of behaviors that can be implemented by different classes.

Example
```java
interface Shape {
    void draw();
}

class Circle implements Shape {
    @Override
    public void draw() {
        System.out.println("Drawing a circle");
    }
}

class Rectangle implements Shape {
    @Override
    public void draw() {
        System.out.println("Drawing a rectangle");
    }
}

public class Main {
    public static void main(String[] args) {
        Shape shape1 = new Circle();
        Shape shape2 = new Rectangle();

        shape1.draw(); // Output: Drawing a circle
        shape2.draw(); // Output: Drawing a rectangle
    }
}
```

Conclusion:

Polymorphism is a powerful feature of Java that enables a single interface to represent different underlying forms (data types). By leveraging polymorphism, Java developers can write more flexible, maintainable, and reusable code, significantly enhancing the development process.