Java @Override - Overriding in Java With Examples

Overriding is a very popular concept in programming languages. Method overriding in Java is the case where we have the same method present in the superclass as well as the subclass. It’s one of the OOPS Concepts to implement runtime polymorphism in Java.

Overriding in Java

Let’s see how we normally override a method in java.

Now we will create a subclass overriding BaseClass doSomething method.

Now let’s create a test class to check how overriding works in java.

Output of the above program is:

Method Overriding In JavaMethod Overriding In Java

How Java Overriding Works?

  • The method signature must be exactly the same in the superclass and the subclass.
  • When the instance is created, subclass constructor must be used.
  • At the compile time, the variable refers to the superclass. However, in runtime, it refers to the subclass object.
  • When the method is called on the variable, it looks like the superclass method will be called. But the method is present in the subclass object, hence it’s called.

Here “bc” is of type BaseClass but at runtime, it’s the object of ChildClass. So when we invoke its doSomething(String str) method, it looks for the method in ChildClass and hence the output.

Will Java Override work with different method signature?

Now let’s change the BaseClass doSomething method like below.

You will notice that compiler won’t throw any warnings/errors and now if you run the test program output will be;

The reason is that BaseClass doSomething(Object str) method is not anymore overridden by the ChildClass. Hence it’s invoking BaseClass implementation. ChildClass is overloading the doSomething() method in this case because the method name is the same but the method signature is different.

Java @Override Annotation

Java 1.5 introduced annotations. Java @Override annotation is one of the default java annotation. When we use this annotation for a method, it tells the compiler that we are trying to override a superclass method.

If you uncomment the @Override annotation in ChildClass, you will get following compile-time error message:

Java @Override annotation benefits

  • Java @Override annotation will make sure any superclass changes in method signature will result in a compile-time error and you will have to do necessary changes to make sure the classes work as expected.
  • It’s better to resolve potential issues at compile time than runtime. So always use java @Override annotation whenever you are trying to override a superclass method.

Overriding vs Overloading

Overriding Overloading
Overriding happens between superclass and subclass Overloading is present in the same class
The method signature must be the same in superclass and subclass. The method name must be the same but method signature must be different.
Identification of the method to be called happens at runtime. We can identify the method to be called at compile-time.
If overriding breaks, it can have adverse effects because the program will compile and run. However, the method from superclass will be called rather than subclass. If we change method name to break overloading, the error will come at compile time. So it’s easy to fix and don’t cause much harm.
Overriding is also called Runtime Polymorphism. Overloading is called as Compile-time Polymorphism.

Summary

Overriding in Java is very popular and you can find many examples in JDK itself. It’s mostly present where we have a base class with default implementation of the method and then the subclass overriding it and implementing it.

Reference: Oracle Documentation

By admin

Leave a Reply