Java - Method Overriding

Java

Share

In this tutorial we will learn about method overriding in Java programming language.

What is method overriding?

When a method in a child class or subclass has the same name and type signature as a method in the parent or superclass then the method in the subclass is said to override the method of the parent class and this is method overriding.

Example

In the following example we have the Parent class and we have the Child class which inherits the parent class.

The Parent class has a greetings method and inside the Child class we also have a greetings method.

So, the greetings method of the child class is overriding the method of the parent class.

class Parent {
  public void greetings() {
    System.out.println("Hello from greetings() method of Parent class.");
  }
}

class Child extends Parent {
  // this "greetings" method of the Child class
  // is overriding the "greetings" method of the
  // parent class
  public void greetings() {
    System.out.println("Hello from greetings() method of Child class.");
  }
}

// the main class
public class Example {
  
  // the main method
  public static void main(String[] args) {

    // creating an object of the Child class
    Child obj = new Child();

    obj.greetings();

  }
}

Output:

$ javac Example.java 
$ java Example
Hello from greetings() method of Child class.

So, in the above output we can see that we are getting output from the greetings method of the Child class and it is overriding the greetings method of the Parent class.

How to access the overridden method of the parent class from the child class?

To access the method of the parent class that is overridden in the child class we have to use the super keyword.

In the following example we are calling the overridden method of the parent class from the child class.

class Parent {
  public void greetings() {
    System.out.println("Hello from greetings() method of Parent class.");
  }
}

class Child extends Parent {
  // this "greetings" method of the Child class
  // is overriding the "greetings" method of the
  // parent class
  public void greetings() {

    // to access the overridden method "greetings"
    // of the Parent class we are using super
    super.greetings();

    System.out.println("Hello from greetings() method of Child class.");
  }
}

// the main class
public class Example {
  
  // the main method
  public static void main(String[] args) {

    // creating an object of the Child class
    Child obj = new Child();

    obj.greetings();

  }
}

Output:

$ javac Example.java 
$ java Example
Hello from greetings() method of Parent class.
Hello from greetings() method of Child class.

So, in the above output we can see that we are getting the output from both the greetings method of the parent class and child class.

Why use method overriding?

Method overriding in Java provides us a way to implement run time polymorphism. So, we create a general class with general method that can be inherited by the child classes and if needed can be overridden.

Share

Recently Updated