Polymorphism

Thinking

Polymorphism allows Java objects to be treated as instances of their parent class rather than their actual class, enabling flexibility and dynamic behavior.

It primarily manifests in two ways: method overloading (compile-time polymorphism) and method overriding (runtime polymorphism), both critical for designing scalable and maintainable applications.

Description

Polymorphism means "many forms" and allows methods to perform different tasks based on the input or the object calling them.

  • Method Overloading: Defining multiple methods with the same name but different parameters within the same class. It’s a compile-time polymorphism.
  • Method Overriding: Redefining a method in a subclass with the same signature as in the superclass. It’s runtime polymorphism and supports dynamic method dispatch.

Polymorphism promotes code reusability, flexibility, and the ability to implement elegant object-oriented designs.

Video Resources

Java Polymorphism Explained

A beginner-friendly explanation of polymorphism in Java with examples.

Examples (code)

Method Overloading Example


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

    int add(int a, int b, int c) {
        return a + b + c;
    }

    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));         // 15
        System.out.println(calc.add(5, 10, 15));     // 30
        System.out.println(calc.add(5.5, 10.5));     // 16.0
    }
}
  

Method Overriding Example


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

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

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

Real-World Applications

Flexible Code Design

Polymorphism enables writing code that works with objects of different classes through a common interface.

Frameworks and APIs

Used in frameworks to allow methods to behave differently based on subclass implementations.

Runtime Method Selection

Supports dynamic method dispatch, allowing method calls to be resolved at runtime for flexible behavior.

Interview Questions

Q1: What is polymorphism in Java?

Show Answer

Polymorphism is the ability of a method to perform different tasks based on the object or input, typically through method overloading and overriding.

Q2: What is the difference between method overloading and method overriding?

Show Answer

Method overloading occurs within the same class with different parameters, while method overriding happens in a subclass redefining a superclass method with the same signature.

Q3: Can you override a static method in Java?

Show Answer

No, static methods belong to the class, and cannot be overridden. They can be hidden but not overridden.