[Java] Method Overriding
Method Overriding
- If subclass has the same method as declared in the parent class, it is known as method overriding
1. Usage
- Method overriding is used to provide specific implementation of a method that is already provided by its super class
- Method overriding is used for runtime polymorphism
2. Rule
- method must have same name as in the parent
- method must have same parameter
3. Problem without method overriding
- Problem is that I have to provide a specific implementation of run() method
4. Example
5. Question
1) Can we override static method?
- No, static method can't be overriden. It can be proved by runtime polymorphism
2) Why we can't overriden static method?
- Static method is bound with class whereas instance method is bound with object
- Static belongs to class area
- instance belongs to heap area