Method Overriding in Java

In this tutorial, we will learn about Method Overriding in java. If we create a method in the child class with the same name as declared in parent class it is known as Method Overloading.

Rules to perform Method Overloading in Java:

  • Method name should be same as declared in Parent class.
  • Parameter of the method should be the same as a method declared in Parent class.

Example Program:

class Master{
   public void show(){
     System.out.println("This is show method in Master class");
   }
   public void show2(){
     System.out.println("This is show2 method in Master class");
   }
}
public class Student extends Master{
    public void show(){
     System.out.println("This is show method in Student class");
    }
    public static void main(String args[]){
      Student obj=new Student();
      obj.show();
      obj.show2();
    }
}

As in the above example, you can see the method show is already present in Master class but we are overriding this method in Student class. It means when we call this method with the help of object of Student class it will invoke the method which is defined inside the student class.

Method overriding is very helpful when we want to change the working of the method which is already defined in Parent class. In this case, with the help Method Overriding, we can Override that particular method in Child class.

Spread the love
Scroll to Top