If the child class has the same method declared as in the parent class,known as “Method Overriding”.
Example :-
class Teacher{ public void work() { System.out.println("Teaching"); } } class Student extends Teacher{ public void work(){ System.out.println("Learning"); } public static void main( String args[]) { Student s = new Student(); s.work(); } }
Output :-
Learning
Here work() is overridden method declared in both Teacher and Student class.
- points of Method overriding :-
- It is used to provide specific execution of a inherited method without modifying the parent class.
- It is used to achieve Run time polymorphism.
Points to remember :-
* We can not override static method because static method is bound with class.
* We can not override private,final methods too.
* We can not override main method because of static method.
* It is different from overloading , where parameters must be different in overloading but not in case of overriding.