C
C
Core Java
Search…
20) Method Overriding

Method overriding

Definition:

Changing the implementation of super class method in the sub class according to the needs of the sub class is called Method overriding.
To achieve Method Overriding 3 things are mandatory,
    1.
    Inheritance.
    2.
    Non-Static Methods.
    3.
    Signature or Methods Declaration should be same.
Whenever we perform Method overriding super class implementation of that method will be lost or masked in the sub class I.e., we will get the latest implementation of sub class.
Declaring a method in subclass which is already present in parent class is known as method overriding.
When a method in a subclass has the same name, same parameters or signature and same return type (or sub-type) as a method in its super-class, then the method in the subclass is said to override the method of super-class.
Why Method Overriding?
Method overriding is one of the way by which java achieve Run Time Polymorphism.
The version of a method that is executed will be determined by the object that is used to invoke it. If an object of a parent class is used to invoke the method, then the version in the parent class will be executed, but if an object of the subclass is used to invoke the method, then the version in the child class will be executed.
Example:
In the below example Boy class extends Human class. Both the classes have a common method void eat(). Boy class is giving its own implementation to the eat() method or in other words it is overriding the method eat().
1
package com.gs.ilp.corejava.methodoverriding;
2
3
class Human {
4
public void eat() {
5
System.out.println("Human is eating");
6
}
7
}
8
9
public class Boy extends Human {
10
public void eat() {
11
System.out.println("Boy is eating");
12
}
13
14
public static void main(String args[]) {
15
Boy obj = new Boy();
16
obj.eat();
17
}
18
}
Copied!
Output:
Boy is eating
Advantage of method overriding
The main advantage of method overriding is that the class can give its own specific implementation to an inherited method without even modifying the parent class (base class).
Rules of method overriding in Java
    1.
    Argument list: The argument list of overriding method must be same as that of the method in parent class. The data types of the arguments and their sequence should be maintained as it is in the overriding method.
    2.
    Access Modifier: The Access Modifier of the overriding method (method of subclass) cannot be more restrictive than the overridden method of parent class. For e.g. if the Access Modifier of base class method is public then the overriding method (child class method ) cannot have private, protected and default Access modifier as all of the three are more restrictive than public.
    3.
    Private, static and final methods cannot be overridden as they are local to the class. However static methods can be re-declared in the sub class, in this case the sub-class method would act differently and will have nothing to do with the same static method of parent class.
    4.
    If a class is extending an abstract class or implementing an interface then it has to override all the abstract methods unless the class itself is an abstract class.
    5.
    We cannot override constructor as parent and child class can never have constructor with same name (Constructor name must always be same as Class name).
    6.
    We can call parent class method in overriding method using super keyword.
Last modified 2yr ago