Polymorphism
Polymorphism
An OOP concept where a single method can take multiple forms, allowing for the definition of different behaviour while sharing the same method name.
The term polymorphism comes from the Greek words "poly"(many) and "morph"(form), meaning many forms.
Types of Polymorphism
Static Polymorphism (Compile-Time)
- Achieved through method overloading.
- It allows multiple methods in the same class to have the same name but different parameter lists.
- The compiler determines which method to invoke based on the method signature.
public class Calculator {
public int add(int a, int b) { // method to add two integers
return a + b;
}
public double add(double a, double b) { // Overloaded method to add two doubles
return a + b;
}
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));
}
}
- Polymorphism is not the same as inheritance.
- While inheritance enables polymorphism, they are distinct concepts.
Dynamic Polymorphism (Run-Time)
- Achieved through method overriding (inheritance).
- The method to be executed is determined at runtime based on the object's type.
class Bird {
public void fly() { System.out.println("This bird can fly."); }
}
class Penguin extends Bird {
@Override
public void fly() { System.out.println("Penguins cannot fly."); }
}
It is very important to distinguish method overloading and method overriding:
| Overloading | Overriding | |
|---|---|---|
| Definition | Same method name with different parameters in the same class | Subclass provides a new version of a method inherited from the parent class |
| Purpose | Provide multiple ways to perform a similar action | Must be the same as parent method. |
| Parameters | Must differ (number or type) | Must be the same as parent method |
| Return type | Can differ if parameters differ. | Must be the same (or compatible) as parent method |
| Requirements | Method should be in the same class or inherited | Requires inheritance |
Advantages of Polymorphism
Code reusability and extensibility:
- Reusability: Polymorphism allows subclasses to inherit and override methods from parent classes, enabling code reuse without modification.
- Extensibility: New subclasses can be added with unique behaviours without altering existing code.
Decoupling and abstraction
- Decoupling: Polymorphism decouples the interface from the implementation, allowing external programs to interact with objects without knowing their specific types.
- Abstraction: A common interface provides a high level of abstraction, hiding implementation details.
- Assume Animal class example, where the makeSound() method is overridden by subclasses like Cat and Dog.
- An external program can call makeSound() without knowing whether the object is a cat or a dog.
Flexibility and maintainability
- Flexibility: Polymorphism enables dynamic behaviour at runtime, allowing objects to be interchanged easily.
- Maintainability: Changes to a parent class automatically propagate to subclasses, reducing maintenance overhead.
- What is polymorphism, and why is it important in object-oriented programming?
- What is the difference between static (compile-time) polymorphism and dynamic (run-time) polymorphism?
- What is the difference between method overriding and overloading?
- What are the main advantages of polymorphism in terms of code reusability, flexibility, and maintainability?
How does polymorphism reflect the way humans categorise and interact with the world? Consider how this concept influences our approach to problem-solving in computer science.