Last Updated: 25 December, 2023
Method overriding can only be achieved through inheritance in Java, which means redefining a method in a child class that is already defined in the parent class. In this way, both parent and child classes have the same method name and the same method parameters. This is known as "method overriding".
The method overriding approach helps the child class change the implementation of the parent class method according to the requirements.
Method overriding provides an approach to achieving Run-Time Polymorphism in Java.
The overriding method must have the same name, parameter list, and return type as the overridden method.
The overriding method cannot be less accessible than the overridden method (e.g., can't be private if the overridden method is public).
In Java, we cannot achieve method overriding with the following types of methods: Private Methods, Static Methods, Final Methods.
Method overriding is a fundamental concept in many design patterns, such as Template Method and Strategy.
Abstract methods are always overridden in the child class.
Let's understand how to implement method overriding in Java with the help of the given example below.
In the above example, the Square class is extending the Shape class and rewriting the draw() method. That means the draw() method is overridden in the child class (Square). When we call the draw() method, only the child class's draw() method is called.
Output
Square class - draw() method
Square class - draw() method
✅ The @Override annotation is a method-level annotation that is used with the overridden method in the child class, but it is not mandatory.
✅ The @Override annotation informs the compiler that the method in the child class is overriding the parent class method.
✅ The overridden method can be called by the child class object or the parent class reference object.
No, because static methods are bonded using static binding at compile time, Java does not allow overriding them.Therefore, we cannot override static methods in Java.
Let's see what will happen if we try to override a static method.
Output
Here, m1() is a static method and is being overridden in the child class B. So, at the time of compiling the code, it is throwing the error message as shown in the output.
No, the final method cannot be overridden in the child class because that is the purpose of the keyword, something that cannot be changed or overridden. The purpose of inheritance and polymorphism is to have objects of the same class implement methods (not the names, but the code in the methods) in different ways.
We must declare methods with the final keyword if we are required to follow the same implementation throughout all the derived classes. By using the final method, we can apply the restriction on inheritance.
Let's see what will happen if we try to override the final method.
Output
As shown in the preceding example, m1() is a final method that is overridden in the child class B. So, at the time of compiling the code, it is throwing the error message as shown in the output.
No, we cannot override private methods in Java because, as we know, private properties cannot be accessed outside the class, so there is no possibility to override private methods in the child class.
Let's see what will happen if we try to override a private method.
Output
Here, m1() is a private method of class A that is overridden in the child class B. So, at the time of compiling the code, it is throwing the error message as shown in the output.
That's all, guys. I hope this Java article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com
What are the benefits of using method overriding in Java?
Ans. Method overriding is only possible in inheritance. The main advantage of method overriding is that the class can give its own specific implementation to an inherited method without even modifying the parent class's method implementation.
This is helpful when a class has several child classes; if a child class needs to use the parent class method, it can use it, and the other classes that want a different implementation can use the overriding feature to make changes without touching the parent class code.
Why can't a static method in Java be overridden?
No, a static method cannot be overridden because static methods belong to a class, not an instance of the class. Only instance methods can participate in method overriding in Java.
Why can the final method not be overridden in Java?
The final method cannot be overridden in the child class because that is the purpose of the keyword, something that cannot be changed or overridden. The purpose of inheritance and polymorphism is to have objects of the same class implement methods (not the names but the code in the methods) in different ways.
Why can a private method not be overridden in Java?
We cannot override private methods in Java because, as we know, private properties cannot be accessed outside the class, so there is no possibility to override private methods in the child class.
Can we override the main method in Java?
No, the main method cannot be overridden as it is a static method, and we know that we cannot override static methods in Java. Static methods belong to a class, and only non-static methods (instance methods) can participate in method overriding operations.