Introduction to Method Overriding:
- Firstly, let’s understand the overriding in object-oriented terms. Overriding means override the functionality of an existing method.
- If the subclass or child class has the same method as we declared in the superclass or parent class, it is called Method overriding in Java.
Use of Method Overriding:
- It is used to give us the implementation of a method that is already provided by its superclass.
- It is used for runtime polymorphism (many forms).
Rules about the Method Overriding:
- First, the method signature, like method name, its parameter list, and also the return type, have to match exactly.
- The method should have the same name as same as the parent class.
- The method must have the same parameter as the same as the parent class has.
Difference between Method Overloading and Method Overriding:
- It is in the same class, where we have more than one method having the same name but different parameters.
- It is one of the methods which is also defined in the subclass as the same in the superclass.
- In this, the parameters of the method remain the same.
Note: Method Overriding is mostly used to achieve Run Time Polymorphism.