in , ,

Java Method Overriding

Java Method Overriding
Java Method Overriding

Java Method Overriding

In this tutorial, we will find out about the method overriding in Java with the help of examples.

In the last tutorial, we learned out about inheritance. Inheritance is an OOP property that allows us to determine another class (subclass) from a current class (superclass). The subclass acquires the attributes and methods for the superclass.

Presently, if a similar method is characterized in both the superclass class and the subclass class, at that point the strategy for the subclass class abrogates the technique for the superclass. This is known as method overriding.


Example 1: Method Overriding

class Animal {
   public void displayInfo() {
      System.out.println("I am an animal.");
   }
}

class Dog extends Animal {
   @Override
   public void displayInfo() {
      System.out.println("I am a dog.");
   }
}

class Main {
   public static void main(String[] args) {
      Dog d1 = new Dog();
      d1.displayInfo();
   }
}

Output

I am a dog.

In the above program, the displayInfo() method is present in both the Animal superclass and the Dog subclass.

When we call displayInfo() using the d1 object (object of the subclass), the method inside the subclass Dog is called. The displayInfo() method of the subclass overrides the same method of the superclass.

Notice the use of the @Override annotation in our example. In Java, annotations are the metadata that we used to give data to the compiler. Here, the @Override annotation determines the compiler that the technique after this comment supersedes the strategy for the superclass.

It isn’t required to use @Override. In any case, when we use this, the method should follow all the rules of overriding. Something else, the compiler will create a mistake.


Java Overriding Rules

  • Both the superclass and the subclass must have a similar technique name, a similar return type, and a similar parameter list.
  • We can’t override the method declared as final and static.
  • We ought to consistently override abstract methods for the superclass (will be discussed in later tutorials).

super Keyword in Java Overriding

A common question that arises while performing overriding in Java is:

Can we access the method of the superclass after overriding?

Well, the answer is Yes. To access the method of the superclass from the subclass, we use the super keyword.

Example 2: Use of super Keyword

class Animal {
   public void displayInfo() {
      System.out.println("I am an animal.");
   }
}

class Dog extends Animal {
   public void displayInfo() {
      super.displayInfo();
      System.out.println("I am a dog.");
   }
}

class Main {
   public static void main(String[] args) {
      Dog d1 = new Dog();
      d1.displayInfo();
   }
}

Output

I am an animal.
I am a dog.

In the above example, the subclass Dog overrides the method displayInfo() of the superclass Animal.

At the point when we call the method displayInfo() using the d1 object of the Dog subclass, the method inside the Dog subclass is called; the method inside the superclass isn’t called.

Inside displayInfo() of the Dog subclass, we have used super.displayInfo() to call displayInfo() of the superclass.


Note that constructors in Java are not acquired. Consequently, there is nothing of the sort as constructor overriding in Java.

However, we can call the constructor of the superclass from its subclasses. For that, we use super().


Access Specifiers in Method Overriding

A similar method announced in the superclass and its subclasses can have diverse access specifiers. In any case, there is a limitation.

We can just use those access specifiers in subclasses that give larger access than the access specifier of the superclass. For instance,

Suppose, a method myClass() in the superclass is declared protected. Then, the same method myClass() in the subclass can be either public or protected, but not private.

Example 3: Access Specifier in Overriding

class Animal {
   protected void displayInfo() {
      System.out.println("I am an animal.");
   }
}

class Dog extends Animal {
   public void displayInfo() {
      System.out.println("I am a dog.");
   }
}

class Main {
   public static void main(String[] args) {
      Dog d1 = new Dog();
      d1.displayInfo();
   }
}

Output

I am a dog.

In the above example, the subclass Dog overrides the method displayInfo() of the superclass Animal.

Whenever we call displayInfo() using the d1 (object of the subclass), the method inside the subclass is called.

Notice that, the displayInfo() is declared protected in the Animal superclass. The same method has the public access specifier in the Dog subclass. This is possible because the public provides larger access than the protected.


Overriding Abstract Methods

In Java, abstract classes are made to be the superclass of different classes. Also, if a class contains an abstract method, it is compulsory to override it.

We will learn more about abstract classes and overriding of abstract methods in later tutorials.


Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.

salman khan

Written by worldofitech

Leave a Reply

Java Inheritance

Java Inheritance

Java super keyword

Java super keyword