• Runtime Polymorphism (Method Overriding): A subclass provides a
specific implementation of a method already defined in its superclass. The
method to be executed is determined at runtime based on the object's actual
type.
o Example:
Java
class Animal {
public void makeSound() { System.out.println("Generic animal
sound"); }
}
class Dog extends Animal {
@Override
public void makeSound() { System.out.println("Woof!"); }
}
class Cat extends Animal {
@Override
public void makeSound() { System.out.println("Meow!"); }
}
Benefits of Polymorphism:
• Flexibility: Code can work with objects of multiple related types without
knowing their specific subclass.
• Reusability: Methods can be written once and used with different object
types.
• Maintainability: Code becomes easier to understand and modify.
• Extensibility: New classes can be added to the hierarchy without modifying
existing code.