English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this tutorial, we will learn about method overriding in Java through examples.
In the previous tutorial, we learned about inheritance. Inheritance is an OOP property that allows us to derive a new class (subclass) from an existing class (superclass), and the subclass inherits the properties and methods of the superclass.
Now, if the same method is defined in both the superclass and the subclass, the method of the subclass will override the method of the superclass. This is called method overriding (method rewriting).
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 exists in both the Animal superclass and the Dog subclass.
When we use d1When an object (an object of the subclass) calls displayInfo(), it will call the method in the subclass Dog. The displayInfo() method of the subclass overrides the same method of the superclass.
Pay attention to the @Override annotation used in our example. In Java, annotations are metadata we use to provide information to the compiler. Here, the @Override annotation tells the compiler that the method following it will override the method of the superclass.
Using @Override is not mandatory. However, when we use this method, it should follow all the rules of overriding. Otherwise, the compiler will generate an error.
Both the superclass and subclass must have the same method name, the same return type, and the same parameter list.
We cannot override methods declared as final and static.
We should always override the abstract methods of the superclass (to be discussed in future tutorials).
A common problem that occurs when overriding in Java is:
Can we access the method of the superclass after overriding?
The answer isAffirmative. To access the method of the superclass from the subclass, we use the 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 displayInfo() method of the superclass Animal.
When we use Dog subclass's d1When the displayInfo() method is called by an object, the method inside the Dog subclass will be called. The method inside the parent class will not be called.
Inside the displayInfo() of the Dog subclass, we use super.displayInfo() to call the displayInfo() of the parent class.
It should be noted that constructors in Java are not inherited. Therefore, there are no issues such as constructor overriding in Java.
However, we can call the constructor of the superclass from its subclass. To do this, we use super(). For more information, please visitJava super keyword.
The same method declared in the parent class and its subclass can have different access specifiers. However, there is a limitation.
We can only use those access specifiers that are used in subclasses that provide greater access privileges than the parent class. For example,
Suppose the method myClass() in the parent class is declared as protected. Then, the same method in the subclass myClass() can be public or protected, but cannot be private.
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 displayInfo() method of the superclass Animal.
Every time we use d1When the displayInfo() is called by an object of the subclass, the method within the subclass is called.
Note that displayInfo() is declared as protected in the Animal superclass. This method has a public access specifier in the Dog subclass. This is allowed because public provides greater access privileges than protected.
In Java, an abstract class is created as a superclass (base class) for other classes. Moreover, if a class contains an abstract method, it must be overridden.
In the following tutorials, we will learn more about the overriding of abstract classes and abstract methods.