Method Overriding:
- Method Overriding is an Object-Oriented Programming (OOP) concept.
- Method overriding allows a child class to redefine a method of the parent class while keeping the same.
- same method name.
- same number of arguments
- same return type in different classes(parent-child)
- same access modifier.
- It is used to achieve runtime polymorphism.
Rules for Method Overriding
- The method must have the same name as in the parent class.
- The method must have the same parameter list.
- The return type should be the same or compatible.
- Only inherited methods can be overridden.
- static, and private methods cannot be overridden.
Advantages
- Achieves runtime polymorphism.
- Allows a subclass to provide a specific implementation.
- Improves code flexibility and reusability.
Example code:
Parent Class
Child Class

Top comments (0)