Yandex

Java Advanced ConceptsJava Advanced Concepts3

Java ReferenceJava Reference1

Polymorphism in Java
Method Overloading and Overriding



In Java, polymorphism is one of the four core principles of object-oriented programming (OOP), alongside inheritance, encapsulation, and abstraction. At its core, polymorphism allows objects to take many forms depending on the context. This dynamic nature leads to more flexible and maintainable code.

There are two main types of polymorphism in Java:

Why Use Polymorphism?

Polymorphism simplifies code reusability, enhances scalability, and allows us to build systems that are easier to extend and modify. Imagine writing a sorting algorithm that works on different types of objects — with polymorphism, that's not just possible, it's elegant.

1. Compile-time Polymorphism (Method Overloading)

Compile-time polymorphism occurs when multiple methods in the same class have the same name but different parameter lists. The decision about which method to call is made by the compiler.

Example: Method Overloading

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

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

    int add(int a, int b, int c) {
        return a + b + c;
    }
    
    public static void main(String[] args) {
        Calculator calc = new Calculator();
        System.out.println(calc.add(2, 3));
        System.out.println(calc.add(2.5, 3.5));
        System.out.println(calc.add(1, 2, 3));
    }
}
5
6.0
6

Explanation:

Here, the method add is overloaded three times. The compiler picks the correct version to invoke based on the number and types of arguments. This helps in writing intuitive APIs while avoiding redundant method names.

2. Runtime Polymorphism (Method Overriding)

Runtime polymorphism happens when a subclass provides a specific implementation of a method that is already defined in its superclass. The method that gets executed is determined at runtime based on the object's actual type.

Example: Method Overriding

class Animal {
    void sound() {
        System.out.println("Some generic animal sound");
    }
}

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

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

public class TestPolymorphism {
    public static void main(String[] args) {
        Animal a;
        a = new Dog();
        a.sound();

        a = new Cat();
        a.sound();
    }
}
Dog barks
Cat meows

Explanation:

Even though the reference variable a is of type Animal, the actual object it points to is either a Dog or a Cat. At runtime, Java determines the real object and invokes the appropriate sound() method. This is known as dynamic method dispatch.

Polymorphism in Action: Real-World Analogy

Imagine you have a remote control. Whether it operates a TV, a fan, or an AC depends on the device it’s currently linked to. The same remote (reference) can behave differently (method call) depending on the actual device (object instance) — this is polymorphism in everyday life.

Benefits of Using Polymorphism

Things to Remember

Quick Recap

Feature Method Overloading Method Overriding
Type Compile-time Runtime
Class Involvement Same class Parent and child classes
Signature Different parameter list Same method signature
Polymorphism Static Dynamic

Conclusion

Java Polymorphism isn’t just a concept—it’s a paradigm shift in how we design programs. With method overloading, you get cleaner, more intuitive code. With method overriding, you get flexibility and adaptability.

QUIZ

Question 1:What type of polymorphism is demonstrated by the following code?
class Printer {
    void print(String msg) {
        System.out.println(msg);
    }

    void print(String msg, int times) {
        for (int i = 0; i < times; i++) {
            System.out.println(msg);
        }
    }
}

Question 2:Method overriding in Java allows static methods to be redefined in child classes.

Question 3:Which of the following are correct about method overloading?

Question 4:What is the output of the following code?
class Animal {
    void sound() {
        System.out.println("Some sound");
    }
}

class Lion extends Animal {
    void sound() {
        System.out.println("Lion roars");
    }
}

public class Zoo {
    public static void main(String[] args) {
        Animal a = new Lion();
        a.sound();
    }
}

Question 5:Constructors in Java can be overloaded, but not overridden.

Question 6:Select all benefits of using polymorphism in object-oriented design:



Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

You can support this website with a contribution of your choice.

When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M