Method Overriding In Java
By AmarSivas | | Updated : 2021-12-12 | Viewed : 247 times

We will explore here Method overriding and some limitations of method overriding where it will be applicable.
Table of Contents:
Method Overriding In Java
When a subclass contains method declaration as same as superclass then that method is said to be overridden. Method overriding means the method of the subclass should be the same as the method of the parent class. Here signature in both classes must be the same. Method overriding allows us to define specific implementation in child class. In other words method declaration in the child class with different implementation as like in parent class then that method is overridden.
Method Overriding Example In Java
public class Vehicle {
public void run(){
System.out.println("Vehicle is running");
}
}
public class Car extends Vehicle {
public void run(){
System.out.println("Car is running");
}
}
Notice here we override the method run() in child class Car from parent class Vehicle. When you call as like below then the child class method will be invoked as dynamic binding.
public class MainClient {
public static void main(String[] args) {
Vehicle vehicle = new Car();
vehicle.run();
}
}
Dynamic Binding In Java
Binding helps to decide which method body to be invoked after the method call. Binding is a relation between a method call with a method body. When a method call statement is placed for the execution then the method body should be invoked for execution. So binding will decide which method body should be invoked.
In java, we have two types. Dynamic binding (Late Binding) and static binding (Early Binding) are binding types in Java. Overriding is Dynamic binding and Overloading is Static Binding.
Overriding in Java is Dynamic binding. When we invoke an Overridden method with Parent class reference then Child's method body is only invoked. Here Parent class method will not be invoked for execution. Binding happens at Run time thus this type of binding is called Dynamic binding.
Notice the above-given example so you can understand that only the child class method will be invoked.
Method Overriding Rules In Java
We will look at different types of rules of overriding where overriding is having limitations.
Overriding Private Method
Overriding is not applicable to private methods. When you try to override then the compiler throws a syntax error.
public class Vehicle {
private void run(){
System.out.println("Vehicle is running");
}
}
public class Car extends Vehicle {
public void run(){
System.out.println("Vehicle is running");
}
}
So when you try to call the Car method with Vehicle reference then we can see the compile-time error.
public class MainClient {
public static void main(String[] args) {
Vehicle vehicle = new Car();
vehicle.run();
}
}
D:\docsconsole-git-master\core-java-master\Java-Metho-Overriding-Example-App\src\main\java\com\docsconsole\tutorials\overridingwithprivate\MainClient.java:6:16
java: run() has private access in com.docsconsole.tutorials.overridingwithprivate.Vehicle
Overriding Final Method
Overriding the final method is not possible. It will throw a compile-time error.
public class Vehicle {
public final void run() {
System.out.println("Vehicle is running");
}
}
public class Car extends Vehicle {
public void run() {
System.out.println("Car is running");
}
}
public class MainClient {
public static void main(String[] args) {
Vehicle vehicle = new Car();
vehicle.run();
}
}
When you run the above Mainclient program you can see the below-given compile-time error.
D:\docsconsole-git-master\core-java-master\Java-Metho-Overriding-Example-App\src\main\java\com\docsconsole\tutorials\overridingwithfinal\Car.java:6:17
java: run() in com.docsconsole.tutorials.overridingwithfinal.Car cannot override run() in com.docsconsole.tutorials.overridingwithfinal.Vehicle
overridden method is final
Overriding Static Method
Overriding is not applicable for static methods as well. When we call child class static method with parent class reference where both classes signature is same then parent class method will be invoked. Here the overriding concept is not applied. Please look at the below given example.
public class Vehicle {
public static void run() {
System.out.println("Vehicle is running");
}
}
public class Car extends Vehicle {
public static void run() {
System.out.println("Car is running");
}
}
public class MainClient {
public static void main(String[] args) {
Vehicle vehicle = new Car();
Vehicle.run();
}
}
Please find the output of this example given below.
Vehicle is running
Overriding Synchronized Method
Overriding is applicable for synchronized methods. The synchronized method can be overridden with the non-synchronized method and vice versa. Notice below given example.
public class Vehicle {
public synchronized void run() {
System.out.println("Vehicle is running");
}
public void run(String fuel) {
System.out.println("Car is running with: " +fuel);
}
}
public class Car extends Vehicle {
public void run() {
System.out.println("Car is running");
}
public synchronized void run(String fuel) {
System.out.println("Car is running with: " +fuel);
}
}
When you run the above example you will get the result with overridden methods.
Overriding Strictfp Method
public class Vehicle {
public strictfp void run() {
System.out.println("Vehicle is running");
}
public void run(String fuel) {
System.out.println("Vehicle is running with: "+ fuel);
}
}
public class Car extends Vehicle {
public void run() {
System.out.println("Car is running");
}
public strictfp void run(String fuel) {
System.out.println("Car is running with: "+ fuel);
}
}
When you execute the above you can get overridden method results.
Overriding With Checked Exception
-
If parent class's method does not throw an exception: When the parent class's method does not throw any exception then the child class's overridden method should not throw any checked exception. But here child class method can throw an unchecked exception. -
If the parent class's method does throw an exception: When parent class's method throws an exception then the child class's method should throw same exception or it's child expression
Now we will look at examples to see both cases.
If parent class''s method does not throw an exception
public class Vehicle {
public void run() {
System.out.println("Vehicle is running");
}
}
public class Car extends Vehicle {
public void run() throws IOException {
System.out.println("Car is running");
}
}
public class Bus extends Vehicle {
public void run() throws NullPointerException {
System.out.println("Bus is running");
}
}
Car method is trying to override the Vehicle's method with a checked exception where a Vehicle does not throw any exception. So here it will provide compile-time error. In another case, Bus is also overriding the same parent. But Bus's run() method is throwing the unchecked exception. Here it will not provide any compile-time error. The overrdden method will be invoked properly.
If the parent class's method does throw an exception
public class Vehicle1 {
public void run() throws NullPointerException {
System.out.println("Vehicle is running");
}
}
public class Bike extends Vehicle1 {
public void run() throws Exception {
System.out.println("Bike is running");
}
}
public class MainClient {
public static void main(String[] args) {
/*Vehicle vehicle = new Car();
vehicle.run();*/
Vehicle1 vehicle1 = new Bike();
vehicle1.run();
}
}
When we try to execute the Main client we will get a compile-time error. Hence Child class's method should have to throw the same exception or child exception. Here Vehicle1 class is throwing the NullPointerException. But Bike class throwing the Exception. Hence we will get the below compile-time error.
D:\docsconsole-git-master\core-java-master\Java-Method-Overriding-Example-App\src\main\java\com\docsconsole\tutorials\overridingcheckedexceptions\Bike.java:5:17
java: run() in com.docsconsole.tutorials.overridingcheckedexceptions.Bike cannot override run() in com.docsconsole.tutorials.overridingcheckedexceptions.Vehicle1
overridden method does not throw java.lang.Exception
Covariant Return Type In Overriding
Covariant types are introduced in Java 1.5 onwards. Covariant type is used in the return type of the methods. Covariant type means same or derived class object. Suppose you are returning the Number in the parent method. The child class's return type should be the same Number class or its child's class. If you return any other classes in the overridden method then you will get some compile-time error.
public class Vehicle {
public Number run() {
System.out.println("Vehicle is running");
return Integer.valueOf(1);
}
}
public class Car extends Vehicle {
public String run() {
System.out.println("Car is running");
return "Test";
}
}
public class MainClient {
public static void main(String[] args) {
Vehicle vehicle = new Car();
vehicle.run();
}
}
The result of MainClient is a compile-time error which is given below.
D:\docsconsole-git-master\core-java-master\Java-Method-Overriding-Example-App\src\main\java\com\docsconsole\tutorials\overridingwithcovariantreturntype\Car.java:6:19
java: run() in com.docsconsole.tutorials.overridingwithcovariantreturntype.Car cannot override run() in com.docsconsole.tutorials.overridingwithcovariantreturntype.Vehicle
return type java.lang.String is not compatible with java.lang.Number
Please find the GitHub repo Java-Method-Overriding-Example-App