Overriding Methods in Java? – Always use @Override annotation

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

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

package com.journaldev.annotations;

public class BaseClass {
    
    public void doSomething(String str){
        System.out.println("Base impl:"+str);
    }
    
}

Now we will create a subclass overriding BaseClass doSomething method.

package com.journaldev.annotations;

public class ChildClass extends BaseClass{

   //@Override
   public void doSomething(String str){
       System.out.println("Child impl:"+str);
   }

}

Notice that @Override annotation is commented as of now.

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

package com.journaldev.annotations;

public class OverrideTest {

    public static void main(String[] args) {
        BaseClass bc = new ChildClass();
        bc.doSomething("override");
    }

}

Output of the above program is:

Child impl:override

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

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

    //Change argument from String to Object
    public void doSomething(Object str){
        System.out.println("Base impl:"+str);
    }

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

Base impl:override

The reason is that BaseClass doSomething(Object str) method is not overridden anymore by ChildClass and hence it’s invoking BaseClass implementation. ChildClass is overloading the doSomething() method in this case.

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

The method doSomething(String) of type ChildClass must override or implement a supertype method

It’s clear that using Override annotation will make sure any Superclass changes in method signature will result in a warning 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 rumtime. So always use @Override annotation whenever you are trying to override a superclass method.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Current ye@r *

Subscribe to JournalDev Newsletter
Get the FREE access to Monthly Newsletter and Free PDF eBooks
*No Spam Guaranteed. By entering your email address, you agree also subscribing to our newsletter.
Oops! - Something went wrong.
Close
Oracle Magazine for Developers Download Now
Exclusive Offer: Citrix™ Whitepaper on Denial of Service Attack Download Now