DED9

Learn Overriding Method In Java

In this tutorial, you will get acquainted with the Java Overriding method with the help of an example. In the last tutorial, we explained 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.

The displayInfo () method in the above program is available in the Animal and Dog subclasses.

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 compiler’s override annotation indicates that the method after this annotation overcomes the superclass method.

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

Java overriding rules

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, the displayInfo () method of the Animal superclass is overridden below the Dog class. It is ignored, and the displayInfo () method is called in the Dog class.

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

We used super inside the displayInfo () subfolder of the Dog class—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 with greater access than the superclass. 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 not 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.

The process inside the subclass is called whenever we call the displayInfo () method using d1.

Note that displayInfo () is protected 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 protection.

Overriding abstract methods

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

Die mobile Version verlassen