Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does @Override mean?

public class NaiveAlien extends Alien {      @Override     public void harvest(){}  } 

I was trying to understand my friend's code, and I do not get the syntax, @Override in the code. What does that do and why do we need in coding? Thanks.

like image 974
Woong-Sup Jung Avatar asked Dec 03 '10 00:12

Woong-Sup Jung


People also ask

What does an override do?

override Add to list Share. You can override or reject a decision if you're more powerful than the person who originally made the decision. And Congress has the power to override or nullify the Presidential veto if they have a two-thirds vote. The word override can be used in a number of contexts.

What does do not override mean?

to refuse to accept or to decide against a previous decision or order: override a decision/veto It takes a two-thirds vote of the House and Senate to override the governor's veto.

What is the best definition of override?

1 : to prevail or take precedence over if, as is often the case, federal constitutional principles override state statutory or common law— H. P. Wilkins. 2 : to set aside by virtue of superior authority overrode the jury's sentencing recommendation especially : annul sense 2 override a veto with the required majority.

What does government override mean?

As the name suggests, a Congressional Override is the process why which legislation is passed even after a President has vetoed the initial passage. For this process to happen, a President must have first officially vetoed legislation. A pocket veto cannot be overridden as the Congressional session has ended.


2 Answers

This feature is called an annotation. @Override is the syntax of using an annotation to let the compiler know, "hey compiler, I'm changing what harvest does in the parent class", then the compiler can immediately say, "dude, you are naming it incorrectly". The compiler won't compile until you name it correctly.

So, without this @Override annotation, the compiler won't error and it will be considered a new method declaration. It would be difficult to recognize the error at this point.

like image 38
nitin1706 Avatar answered Oct 05 '22 23:10

nitin1706


It's a hint for the compiler to let it know that you're overriding the method of a parent class (or interface in Java 6).

If the compiler detects that there IS no function to override, it will warn you (or error).

This is extremely useful to quickly identify typos or API changes. Say you're trying to override your parent class' method harvest() but spell it harvset(), your program will silently call the base class, and without @Override, you wouldn't have any warning about that.

Likewise, if you're using a library, and in version 2 of the library, harvest() has been modified to take an integer parameter, you would no longer override it. Again, @Override would quickly tell you.

like image 109
EboMike Avatar answered Oct 06 '22 00:10

EboMike