⬅ Previous Topic
Interfaces in JavaNext Topic ⮕
Encapsulation in Java⬅ Previous Topic
Interfaces in JavaNext Topic ⮕
Encapsulation in JavaIn 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:
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.
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.
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
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.
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.
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
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.
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.
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 |
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.
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);
}
}
}
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();
}
}
⬅ Previous Topic
Interfaces in JavaNext Topic ⮕
Encapsulation in JavaYou 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.