Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When do you use Java's @Override annotation and why?

What are the best practices for using Java's @Override annotation and why?

It seems like it would be overkill to mark every single overridden method with the @Override annotation. Are there certain programming situations that call for using the @Override and others that should never use the @Override?

like image 310
Alex B Avatar asked Sep 18 '08 16:09

Alex B


People also ask

Why do we use @override annotation?

@Override @Override annotation informs the compiler that the element is meant to override an element declared in a superclass. Overriding methods will be discussed in Interfaces and Inheritance. While it is not required to use this annotation when overriding a method, it helps to prevent errors.

What is the best reason to apply @override to a method?

The annotation @Override is used for helping to check whether the developer what to override the correct method in the parent class or interface.

Why is @override used in Java?

In Java, method overriding occurs when a subclass (child class) has the same method as the parent class. In other words, method overriding occurs when a subclass provides a particular implementation of a method declared by one of its parent classes.

Why is it important to include @override in front of an overridden method?

Why is it important to include @Override in front of an overridden method? The complier will issue an error if it does not find a corresponding method to override.

Why do we need override?

The benefit of overriding is: ability to define a behavior that's specific to the subclass type, which means a subclass can implement a parent class method based on its requirement. In object-oriented terms, overriding means to override the functionality of an existing method.

Is it necessary to write @override annotation of overridden method?

@Override annotation is used when we override a method in sub class. Generally novice developers overlook this feature as it is not mandatory to use this annotation while overriding the method.


2 Answers

Use it every time you override a method for two benefits. Do it so that you can take advantage of the compiler checking to make sure you actually are overriding a method when you think you are. This way, if you make a common mistake of misspelling a method name or not correctly matching the parameters, you will be warned that you method does not actually override as you think it does. Secondly, it makes your code easier to understand because it is more obvious when methods are overwritten.

Additionally, in Java 1.6 you can use it to mark when a method implements an interface for the same benefits. I think it would be better to have a separate annotation (like @Implements), but it's better than nothing.

like image 190
Dave L. Avatar answered Oct 20 '22 05:10

Dave L.


I think it is most useful as a compile-time reminder that the intention of the method is to override a parent method. As an example:

protected boolean displaySensitiveInformation() {   return false; } 

You will often see something like the above method that overrides a method in the base class. This is an important implementation detail of this class -- we don't want sensitive information to be displayed.

Suppose this method is changed in the parent class to

protected boolean displaySensitiveInformation(Context context) {   return true; } 

This change will not cause any compile time errors or warnings - but it completely changes the intended behavior of the subclass.

To answer your question: you should use the @Override annotation if the lack of a method with the same signature in a superclass is indicative of a bug.

like image 32
jon Avatar answered Oct 20 '22 06:10

jon