Polymorphism
Polymorphism is a key concept in Object-Oriented Programming (OOP) that allows objects of different types to be treated as objects of a common type. It enables flexibility and extensibility in code by allowing a single interface (method or operator) to represent different behaviors in different contexts. Polymorphism can be achieved through method overloading and method overriding.
Types of Polymorphism:
- Compile-Time (Static) Polymorphism:
- Method Overloading: Multiple methods in the same class with the same name but different parameter lists.
class MathOperations { int add(int a, int b) { return a + b; } double add(double a, double b) { return a + b; } }
- Operator Overloading: Extending the use of operators to work with user-defined data types.
- Run-Time (Dynamic) Polymorphism:
- Method Overriding: Providing a specific implementation for a method in a subclass that is already defined in its superclass.
class Animal { void makeSound() { System.out.println("Some generic sound"); } } class Dog extends Animal { @Override void makeSound() { System.out.println("Bark! Bark!"); } }
- Interfaces and Abstract Classes: Defining methods in interfaces or abstract classes and providing specific implementations in concrete classes.
interface Shape { void draw(); } class Circle implements Shape { @Override void draw() { System.out.println("Drawing a circle"); } } class Square implements Shape { @Override void draw() { System.out.println("Drawing a square"); } }
Method Overriding:
Method overriding occurs when a subclass provides a specific implementation for a method that is already defined in its superclass. The @Override
annotation is used to indicate that a method is intended to override a method in the superclass.
class Animal {
void makeSound() {
System.out.println("Some generic sound");
}
}
class Dog extends Animal {
@Override
void makeSound() {
System.out.println("Bark! Bark!");
}
}
Polymorphic Behavior:
Polymorphism allows objects of different classes to be treated as objects of a common type. This is achieved through the use of a common interface or superclass.
public class PolymorphismExample {
public static void main(String[] args) {
Animal myAnimal = new Dog(); // Polymorphic behavior
myAnimal.makeSound(); // Calls the makeSound method of Dog
}
}
In this example, myAnimal
is declared as type Animal
, but it refers to an object of type Dog
. At runtime, the overridden method in Dog
is invoked.
Benefits of Polymorphism:
- Code Reusability: Polymorphism allows the same code (method or interface) to be used with different types of objects.
- Flexibility: Code can be more flexible and extensible, accommodating new classes without modifying existing code.
- Enhanced Readability: Polymorphic code is often more readable and maintainable.
Polymorphism is a powerful concept that contributes to the flexibility and maintainability of code in object-oriented programming languages like Java. It encourages a more modular and extensible design by allowing for a single interface to represent multiple implementations.