JAVA Tutorial



POLYMORPHISM IN JAVA


Polymorphism in Java

Polymorphism is a fundamental concept in Object-Oriented Programming (OOP), allowing objects of different classes to be treated as objects of a common superclass. It is the ability of an object to take on multiple forms, enabling a single method to behave differently based on the object’s class.

What is Polymorphism?

Polymorphism in Java means "many shapes," and it allows one interface to be used for a general class of actions. The specific action is determined by the exact nature of the situation. Polymorphism can be classified into two types:

  • Compile-Time Polymorphism (Method Overloading): This type of polymorphism is resolved during compile time. It occurs when two or more methods in the same class have the same name but different parameter lists.
  • Runtime Polymorphism (Method Overriding): This type of polymorphism is resolved during runtime. It occurs when a method in a subclass overrides a method in the superclass.

Why is Polymorphism Important?

Polymorphism enhances the flexibility and extensibility of the code. It allows developers to write more generic code, which works with objects of different types, thus reducing complexity and increasing reusability.

Types of Polymorphism in Java

There are two types of polymorphism in Java:

  • Compile-Time Polymorphism (Method Overloading): Achieved through method overloading. The method to be called is determined at compile time based on the number or type of arguments passed.
  • Runtime Polymorphism (Method Overriding): Achieved through method overriding. The method to be called is determined at runtime based on the object being referred to.

1. Compile-Time Polymorphism (Method Overloading)

Method overloading is an example of compile-time polymorphism, where two or more methods in the same class share the same name but differ in the number or type of parameters.

class Calculator {
    // Method overloading: same method name but different parameters
    int add(int a, int b) {
        return a + b;
    }

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

public class Main {
    public static void main(String[] args) {
        Calculator calc = new Calculator();
        System.out.println(calc.add(5, 10));  // Calls add(int, int)
        System.out.println(calc.add(5.5, 10.5)); // Calls add(double, double)
    }
}
  

In this example, the add() method is overloaded. Depending on the type of arguments passed, the appropriate method version is called at compile time.

2. Runtime Polymorphism (Method Overriding)

Method overriding is an example of runtime polymorphism. It occurs when a subclass provides a specific implementation of a method already defined in its superclass.

class Animal {
    void sound() {
        System.out.println("Animals make a sound.");
    }
}

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

public class Main {
    public static void main(String[] args) {
        Animal animal = new Dog();  // Animal reference, Dog object
        animal.sound();  // Calls Dog's overridden sound() method
    }
}
  

In this example, the sound() method is overridden in the Dog class. Despite the reference being of type Animal, the Dog class's method is invoked, demonstrating runtime polymorphism.

How Polymorphism Works in Java?

Polymorphism works by allowing one interface to be used for different underlying forms (data types). It enables the same method or class to perform different tasks based on the object it is called on.

Consider the following scenario:

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

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

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

public class Main {
    public static void main(String[] args) {
        Animal myAnimal = new Animal();
        myAnimal.makeSound();  // Animal makes a sound

        myAnimal = new Dog();
        myAnimal.makeSound();  // Dog barks

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

In this case, the makeSound() method demonstrates polymorphism. Even though the reference is of type Animal, the appropriate Dog or Cat class method is invoked based on the object being referenced, showing runtime polymorphism.

Advantages of Polymorphism

  • Code Reusability: Polymorphism allows code to be more reusable and flexible, as the same code can work with objects of different classes.
  • Maintainability: Polymorphism helps in keeping code more organized and easier to maintain because changes to method behavior are localized to the class implementing the method.
  • Extensibility: Polymorphism makes the code more extensible. You can add new classes and objects without modifying the existing code structure.

Polymorphism with Interfaces

In Java, interfaces are also a form of polymorphism. When a class implements an interface, it can override the methods of that interface. This allows different classes to implement the same interface and be treated uniformly.

interface Animal {
    void makeSound();
}

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

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

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

        Animal myCat = new Cat();
        myCat.makeSound();  // Cat meows
    }
}
  

In this case, both Dog and Cat implement the Animal interface, and we can treat both as Animal types, demonstrating polymorphism with interfaces.

Conclusion

Polymorphism is a powerful feature of Java that allows methods and objects to behave in different ways based on their actual class. It promotes flexibility, reusability, and maintainability in object-oriented programming. Whether through method overloading (compile-time polymorphism) or method overriding (runtime polymorphism), polymorphism enhances code efficiency and design simplicity.

Quick Tip:

Remember, polymorphism allows you to invoke the same method on objects of different types, providing flexibility in how your code interacts with those objects.


🌟 Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review