Loading

Polymorphism is one of the four key pillar of Object-Oriented Programming (OOP) in Java, along with Encapsulation, Abstraction, and InheritancePolymorphism comprises two words poly and morphism.


  •  Poly means many
  •  Morphism means forms
Therefore polymorphism means many forms. i.e. method has the same name but different or many functionalities.

Examples: Take a real-life example of a person, at the same time a person can be a student, a teacher, an employee, a son, a father, etc. The person is the same but his behavior or functionality is different in different situations.

There are two types of polymorphism:

1. Compile time Polymorphism
2. Runtime Polymorphism

We can achieve polymorphism in two ways:

  • Method overloading
  • Method overriding

1. Compile-Time Polymorphism (Method Overloading):

Compile time polymorphism is also called Method Overloading. This occurs when multiple methods have same name but different parameters. The Java compiler decides which method to execute at compile time based on the method signature.

Example:
class MathOperations { // Method to add two integers int add(int a, int b) { return a + b; } // Method to add three integers int add(int a, int b, int c) { return a + b + c; } // Method to add two double numbers double add(double a, double b) { return a + b; } public static void main(String[] args) { MathOperations math = new MathOperations(); System.out.println(math.add(5, 10)); // Calls method with two int parameters System.out.println(math.add(5, 10, 15)); // Calls method with three int parameters System.out.println(math.add(5.5, 2.5)); // Calls method with double parameters } }

Output:
15 30 8.0

As we see in the above example, Java identifies which method to call based on the number and type of arguments.

Key Point of Method Overloading

  • Methods must have the same name but different parameters.
  • It helps in code reusability.
  • The return type may or may not be different.
  • It occurs within the same class.

2. Runtime Polymorphism (Method Overriding)

Runtime polymorphism is also called Method Overriding. This occurs when a child class provides a specific implementation for a method already defined in the parent class.

Example:
class Animal { void makeSound() { System.out.println("Animal makes a sound"); } } class Dog extends Animal { @Override void makeSound() { System.out.println("Dog barks"); } } class Cat extends Animal { @Override void makeSound() { System.out.println("Cat meows"); } } public class Main { public static void main(String[] args) { Animal myAnimal; // Reference variable of parent class myAnimal = new Dog(); myAnimal.makeSound(); // Calls Dog's version of makeSound() myAnimal = new Cat(); myAnimal.makeSound(); // Calls Cat's version of makeSound() } }

Output:
Dog barks Cat meows

Here the method makesound() is overridden by Dog and Cat classes. At runtime, Java determines which method to execute based on the object.

Key Point of Method Overriding

  • Methods must have the same name and parameters.
  • It is used to provide specific behavior to a subclass.
  • Achieved through inheritance.
  • Uses dynamic method dispatch (determined at runtime).
  • The return type can be the same or a subclass of the parent method's return type (Covariant return types).

Method Overloading Vs. Method Overriding

FeatureMethod OverloadingMethod Overriding
Definition
Multiple methods with the same name but different parameters.

Child class redefines a method of the parent class.

When it happens
Compile-time

Runtime
ParametersMust be differentMust be the same
Return TypeCan be differentMust be the same or a subclass type
Inheritance Needed ?NoYes (Must be in a subclass)
Method Signature
Must be different
Must be the same

Why is Polymorphism Important ?

Polymorphism play a important role in Java development because of below reasons:

  • Code Reusability: Methods can perform different tasks with the same name.
  • Flexibility: Allows objects to be treated as instances of their parent class.
  • Scalability: New functionality can be added without modifying existing code.
  • Reduces Code Duplication: One method can work for different data types or classes.
Two Minute Drill

Polymorphism is a powerful concepts in java that allow one interface, multiple implementations. It helps in making programs more efficient, readable, and scalable.

  • Compile Time Polymorphism (Method Overloading) allows multiple methods with same name but different parameters.
  • Run Time Polymorphism (Method Overriding) allows a subclass to provide a specific implementation of a parent class method.