Skip to main content

Polymorphism in Java

Banner java icon

๐ŸŽญ Polymorphism in Java - The Shape-Shifting Superpowerโ€‹

Polymorphism in Java is like a master of disguiseโ€”it allows a class to behave differently in different contexts. It's one of the four pillars of object-oriented programming (OOP), along with inheritance, abstraction, and encapsulation. Think of it as an actor playing different roles in different movies! ๐ŸŽฌ


๐Ÿค” What is Polymorphism?โ€‹

Polymorphism lets a class act like a chameleon, adapting to different scenarios. Imagine a superhero who wears different costumes but still remains the same person inside! ๐Ÿฆธโ€โ™‚๏ธ

In Java, we can relate polymorphism to one interface having multiple implementations. The contract remains the same, but each class implements it differently.

For example, a reference variable of a superclass can refer to an instance of a subclass:

Object o = new Object(); // o can hold the reference of any subtype

Object o = new String();
Object o = new Integer();

Here, String and Integer are subclasses of Object. It's like calling someone "Human" whether they are an athlete, a scientist, or a musician! ๐ŸŽถ๐Ÿƒโ€โ™‚๏ธ๐Ÿ”ฌ


๐Ÿ”€ Types of Polymorphismโ€‹

Java polymorphism comes in two flavors:

  1. Compile-time polymorphism (a.k.a. method overloading, static binding)
  2. Runtime polymorphism (a.k.a. method overriding, dynamic binding)

These types are specific to Java, though polymorphism exists in many forms across different programming languages.


๐Ÿ—๏ธ 1. Compile-Time Polymorphism (Method Overloading)โ€‹

As the name suggests, in compile-time polymorphism, the method to be executed is determined at compile time. It is achieved using method overloading.

โœจ How does it work?โ€‹

An object can have multiple methods with the same name, but different:

  • Number of parameters
  • Parameter types

๐Ÿ”ข Example: Calculator Classโ€‹

public class Calculator {

public Integer sum(Integer a, Integer b) {
return a + b;
}

public Float sum(Float a, Float b) {
return a + b;
}

public Double sum(Double a, Double b) {
return a + b;
}
}

Now, when we invoke sum(), the compiler decides which method to call based on argument types. It's like a waiter knowing whether you want coffee โ˜• or tea ๐Ÿต based on your order!

Calculator calc = new Calculator();
Integer sum1 = calc.sum(1 ,2);
Float sum2 = calc.sum(1f ,2f);
Double sum3 = calc.sum(1d ,2d);

โณ 2. Runtime Polymorphism (Method Overriding)โ€‹

Runtime polymorphism is all about method overriding. The method that gets executed is determined at runtime, based on the actual instance of the object.

๐Ÿพ Example: Animal Kingdom ๐Ÿถ๐Ÿฑโ€‹

Think of an Animal class with a makeNoise() method. Dogs ๐Ÿ• bark, cats ๐Ÿˆ meow, but both are still animals!

class Animal {
public void makeNoise() {
System.out.println("Some sound");
}
}

class Dog extends Animal {
public void makeNoise() {
System.out.println("Bark");
}
}

class Cat extends Animal {
public void makeNoise() {
System.out.println("Meow");
}
}

๐Ÿ›Ž๏ธ Calling the Methodโ€‹

At runtime, Java decides which method to invoke based on the actual instance:

Animal cat = new Cat();
cat.makeNoise(); // Prints "Meow"

Animal dog = new Dog();
dog.makeNoise(); // Prints "Bark"

This is like a remote control! ๐ŸŽฎ The button is the same, but whether you control a toy car ๐Ÿš— or a drone ๐Ÿš depends on the actual object!


๐ŸŽฏ Conclusionโ€‹

  • Polymorphism allows objects, methods, and variables to take multiple forms.
  • Java supports method overloading (compile-time) and method overriding (runtime) polymorphism.
  • Operator overloading exists in Java only for the + operator (e.g., adding numbers vs. concatenating strings).

Polymorphism makes Java flexible, scalable, and powerfulโ€”just like a superhero with multiple disguises! ๐Ÿฆธโ€โ™€๏ธ๐Ÿ’ช

Happy Coding! ๐Ÿš€๐Ÿ˜ƒ