blog posts

Learn Overriding Method In Java (In Very Simple Language)

In this tutorial, with the help of an example, you will get acquainted with the Java Overriding method. In the last tutorial, we explained about inheritance. 

Inheritance is an OOP attribute that allows us to derive a new (sub-class) from an existing class (superclass or main class). The subclass inherits the features and methods of the superclass.

Now, if the same method is defined in the superclass and subclass, the subclass method ignores the superclass method, which is called the overriding method.

Example 1: Overriding method

  1. class Animal {
  2. public void displayInfo () {
  3. System.out.println (“I am an animal.”);
  4. }
  5. }
  6. class Dog extends Animal {
  7. @override
  8. public void displayInfo () {
  9. System.out.println (“I am a dog.”);
  10. }
  11. }
  12. class Main {
  13. public static void main (String [] args) {
  14. Dog d1 = new Dog ();
  15. d1.displayInfo ();
  16. }
  17. }

Output

I am a dog.

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

When we call displayInfo (object) using the d1 (sub-object), the method inside the Dog subclass is called. The displayInfo () method of the Animal superclass is ignored.

C: \ Users \ Mr \ Desktop \ method-overriding-in-java.png

In the example above, consider using override annotations. In Java, annotations are metadata that provide information to the compiler. Here, the override annotation for the compiler indicates that the method after this annotation indicates overcoming the superclass method.

It is not necessary to use override. However, when we use it, the method must follow all the important rules. Otherwise, the compiler gives an error.

Java overriding rules

  • Both the superclass and the subclass must have a method with the same name, return type and list of the same parameters.
  • We can not ignore methods called final and static.
  • We must always ignore the abstract methods of the superclass.

Super keyword in Java Overriding

A common question that arises when overriding in Java is:

Can we access the superclass method after Overriding?

The answer is yes. To access the superclass method from the subclass, we use the super keyword.

Example 2: Using the super keyword

  1. class Animal {
  2. public void displayInfo () {
  3. System.out.println (“I am an animal.”);
  4. }
  5. }
  6. class Dog extends Animal {
  7. public void displayInfo () {
  8. super.displayInfo ();
  9. System.out.println (“I am a dog.”);
  10. }
  11. }
  12. class Main {
  13. public static void main (String [] args) {
  14. Dog d1 = new Dog ();
  15. d1.displayInfo ();
  16. }
  17. }

Output

I am an animal.

I am a dog.

In the example above, below the Dog class, the displayInfo () method of the Animal superclass is override. That is, it is ignored and the displayInfo () method is called in the Dog class.

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

Inside the displayInfo () subfolder of the Dog class, we used super.displayInfo () to call the displayInfo () class from the superclass.

It is important to note that builders are not inheritable in Java. Hence, there is no such thing as constructor overriding in Java.

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

Access levels in the overriding method

The methods of the same name defined in the superclass and subclass can have different types of access. However, there is a limit.

We can only use sub-class access attributes that have greater access than the superclass access attribute. For example,

Suppose a myClass () method in the superclass is protected. Then, the same myClass () method under the subclass can be made public or protected but cannot be private.

Example 3: Access levels in Overriding

  1. class Animal {
  2. protected void displayInfo () {
  3. System.out.println (“I am an animal.”);
  4. }
  5. }
  6. class Dog extends Animal {
  7. public void displayInfo () {
  8. System.out.println (“I am a dog.”);
  9. }
  10. }
  11. class Main {
  12. public static void main (String [] args) {
  13. Dog d1 = new Dog ();
  14. d1.displayInfo ();
  15. }
  16. }

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

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

Note that displayInfo () is protecting in the Animal superclass. In the same method, there is a public access attribute under the Dog class. This is possible because of greater access to protected.

overriding abstract methods

In Java, abstract classes are created as superclasses to other classes. If a class contains an abstract method, it must be ignored.