What is the use of Override annotation in Java When do we use it

0 votes
I recently came across the Java's @Override annotation. Why do we use it ? and What are the best practices for using it ? 

Do we mark every single overridden method with the @Overrideannotation or Are there certain programming situations that call for using the @Override ? Also are there situations where we should never use the @Override?
May 4, 2018 in Java by Parth
• 4,640 points

edited May 4, 2018 by Parth 3,470 views

2 answers to this question.

0 votes
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.
answered May 4, 2018 by Akrati
• 3,190 points
0 votes

@Override annotation is used when we override a method in sub class. 

Using @Override annotation while overriding a method is considered as a best practice for coding in java because of the following two advantages:

  •  If programmer makes any mistake such as wrong method name, wrong parameter types while overriding, you would get a compile time error. As by using this annotation you instruct compiler that you are overriding this method. If you don’t use the annotation then the sub class method would behave as a new method (not the overriding method) in sub class.

  • It improves the readability of the code. So if you change the signature of overridden method then all the sub classes that overrides the particular method would throw a compilation error, which would eventually help you to change the signature in the sub classes.

answered Aug 14, 2019 by Sirajul
• 59,230 points

Related Questions In Java

0 votes
1 answer

What is the use of StringBuilder in java?

If you use String concatenation in a ...READ MORE

answered Jun 19, 2018 in Java by Akrati
• 960 points
803 views
0 votes
1 answer

What is the use of assert keyword in java?

Java assertion feature allows the developer to ...READ MORE

answered Jun 28, 2018 in Java by scarlett
• 1,290 points
860 views
0 votes
1 answer

What is the concept of Immutability for strings in Java ? Why are strings immutable ?

According to Effective Java, chapter 4, page 73, ...READ MORE

answered May 11, 2018 in Java by Rishabh
• 3,620 points
1,600 views
0 votes
1 answer

What is the equivalent of the C++ Pair<L,R> in Java?

In a thread on comp.lang.java.help, Hunter Gratzner ...READ MORE

answered Jul 4, 2018 in Java by Rishabh
• 3,620 points
1,074 views
0 votes
2 answers

“Could not find or load main class” mean?

Use the final modifier to enforce good initialization. Avoid returning ...READ MORE

answered Sep 18, 2018 in Java by Sushmita
• 6,920 points
4,514 views
0 votes
2 answers

What is the use of toString method in Java and how can I use it ?

Whenever you require to explore the constructor ...READ MORE

answered Aug 23, 2018 in Java by Daisy
• 8,140 points
4,102 views
0 votes
2 answers

How do I replace character from string at specific indexes?

You could turn the String into a ...READ MORE

answered Aug 22, 2019 in Java by Sirajul
• 59,230 points
23,772 views
0 votes
2 answers

When to use Static Methods in Java?

A static method has two main purposes: For ...READ MORE

answered Aug 10, 2018 in Java by samarth295
• 2,220 points
16,066 views
0 votes
2 answers

What is the use of final class in java?

In Java, items with the final modifier cannot be ...READ MORE

answered Oct 5, 2018 in Java by Daisy
• 8,140 points
1,886 views
0 votes
2 answers

What is the difference between Set and List in java?

List is an ordered sequence of elements. ...READ MORE

answered Apr 26, 2018 in Java by Akrati
• 3,190 points
63,360 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP