Java Method Overriding - The Coding Shala

Home >> Learn Java >> Java Method Overriding

Java Method Overriding

We know Inheritance is useful in Java. A subclass inherits all properties of super class so we don't need to write methods again but sometimes we need to provide the specific implementation of a method which is already provided by its superclass. If a child class has the same method as declared in the parent class then it is known as method overriding in Java.
Java Method Overriding - The Coding Shala
Point to remember: Method overriding is used to achieve runtime polymorphism(will see in later chapters).

Rules for Java Method Overriding

The following are some rules to follow to achieve the method overriding:
  • The method must have the same name as in the parent class.
  • The method must have the same parameter as in the parent class.
  • There must be an IS-A relationship(Inheritance).

Example of Method Overriding

The following is a simple example of method overriding in java:


//method overriding 
//thecodingshala.com 

class Parent{
    void Display(){
        System.out.println("This is parent class");
    }
}

class Child extends Parent{
    //This method override the Parent Display
    @Override
    void Display(){
        System.out.println("This is child class");
    }
}

class Main{
    public static void main(String[] args){
        //Here Parent type reference to Parent Obejct
        //calls parent Display
        Parent p1 = new Parent();
        p1.Display();
        //Here Parent type reference to Child Object
        //calls child's Display
        //this is called run time polymorphism
        Parent p2 = new Child();
        p2.Display();
    }
}
Output:
This is parent class
This is child class

NOTE: Here @Override annotation is not required to use. It informs the compiler that the element is meant to override. It helps to prevent errors.

Private and Final methods can not be overridden. We will get compile-time errors. The following example explains it:

//method overriding 
//thecodingshala.com 

class Parent{
    final void Display(){
        System.out.println("This is parent class");
    }
}

class Child extends Parent{
    //This method override the Parent Display
    @Override
    public void Display(){
        System.out.println("This is child class");
    }
}

class Main{
    public static void main(String[] args){
        Parent p1 = new Parent();
        p1.Display();
        Parent p2 = new Child();
        p2.Display();
    }
}
Output:

prog.java:13: error: Display() in Child cannot override Display() in Parent
    public void Display(){
                ^
  overridden method is final

Protected methods can be overridden and can be made public. The following example explains it:

//method overriding 
//thecodingshala.com 

class Parent{
    protected void Display(){
        System.out.println("This is parent class");
    }
}

class Child extends Parent{
    //This method override the Parent Display
    @Override
    public void Display(){
        System.out.println("This is child class");
    }
}

class Main{
    public static void main(String[] args){
        Parent p1 = new Parent();
        p1.Display();
        Parent p2 = new Child();
        p2.Display();
    }
}
Output:

This is parent class
This is child class

Static methods can not be Overridden. The main() method is also static method so main() also can not be overridden. 

We can declare static methods with the same signature in the subclass as a static method in base class but it is not considered overriding. The method in the derived class hides the method in the base class. It is known as method hiding.

The following example shows method hiding in java:

//method overriding 
//thecodingshala.com 
//static method overriding 
//method hiding
class Parent{
    public static void Display(){
        System.out.println("This is parent class's Static method");
    }
}

class Child extends Parent{
    //This method should override Parent class method as par overriding
    public static void Display(){
        System.out.println("This is child class's static method");
    }
}

class Main{
    public static void main(String[] args){
        Parent p = new Child();
        //this should display child's Display as par rule of overridding
        //but this print child's Display 
        //this is called method hiding
        p.Display();
    }
}
Output:

This is parent class's Static method

We can also call the parent class's method in the Overriding method using a super keyword. The following example shows it:

//method overriding 
//thecodingshala.com 
class Parent{
    public void Display(){
        System.out.println("This is parent class's Static method");
    }
}

class Child extends Parent{
    public void Display(){
        super.Display();
        System.out.println("This is child class's static method");
    }
}

class Main{
    public static void main(String[] args){
        Parent p = new Child();
        p.Display();
    }
}
Output:

This is parent class's Static method
This is child class's static method

NOTE: Constructor can not be overridden. The constructor name must always be the same as the class name.



Other Posts You May Like
Please leave a comment below if you like this post or found some error, it will help me to improve my content.

Comments

Popular Posts from this Blog

Anti Diagonals - The Coding Shala

Shell Script to find sum, product and average of given numbers - The Coding Shala

LeetCode - Bulb Switcher Solution - The Coding Shala

New Year Chaos Solution - The Coding Shala

Sorting the Sentence LeetCode Solution - The Coding Shala