What is Polymorphism?
Polymorphism in Java is a core concept of Object-Oriented Programming (OOP) that allows objects of different classes to be treated as objects of a common superclass. It enables one interface to be used for a general class of actions, with the specific action determined by the exact nature of the situation.
Types of Polymorphism #
- Compile-Time Polymorphism (Method Overloading)
- Run-Time Polymorphism (Method Overriding)
1. Compile-Time Polymorphism (Method Overloading) #
Definition: Compile-time polymorphism, also known as method overloading, occurs when multiple methods in the same class have the same name but different parameter lists. The method that is called is determined at compile time based on the method signature.
Example:
class Printer {
// Method to print an integer
void print(int a) {
System.out.println("Integer: " + a);
}
// Method to print a double
void print(double a) {
System.out.println("Double: " + a);
}
// Method to print a string
void print(String a) {
System.out.println("String: " + a);
}
}
public class Main {
public static void main(String[] args) {
Printer p = new Printer();
p.print(10); // Calls print(int)
p.print(10.5); // Calls print(double)
p.print("Hello"); // Calls print(String)
}
}
Output:
Integer: 10
Double : Double
Double : Hello
In this example, the print method is overloaded with different parameter types. The appropriate method is selected based on the argument passed to it.
2. Run-Time Polymorphism (Method Overriding) #
Definition: Run-time polymorphism, also known as method overriding, occurs when a subclass provides a specific implementation of a method that is already defined in its superclass. The method to be executed is determined at runtime based on the object type.
Example:
// Superclass
class Animal {
void makeSound() {
System.out.println("Animal makes a sound");
}
}
// Subclass 1
class Dog extends Animal {
@Override
void makeSound() {
System.out.println("Dog barks");
}
}
// Subclass 2
class Cat extends Animal {
@Override
void makeSound() {
System.out.println("Cat meows");
}
}
public class Main {
public static void main(String[] args) {
Animal myAnimal;
myAnimal = new Dog();
myAnimal.makeSound();
myAnimal = new Cat();
myAnimal.makeSound();
}
}
Output:
Dog barks
Cat meows
In this example, makeSound is overridden in both Dog and Cat subclasses. At runtime, the method of the actual object type (either Dog or Cat) is called, even though the reference is of type Animal.
Summary #
- Compile-Time Polymorphism (Method Overloading): Achieved by defining multiple methods with the same name but different parameters within the same class. The method to be invoked is determined at compile time.
- Run-Time Polymorphism (Method Overriding): Achieved by overriding a superclass method in a subclass. The method to be invoked is determined at runtime based on the actual object type.
Polymorphism is a powerful feature that enhances flexibility and maintainability in code, allowing for more generic and reusable code.