What does @Override mean?

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

I tried to understand my friend's code, and I am not getting the @Override syntax in the code. What does this do and why do we need coding? Thank.

+66
java override annotations
Dec 03 2018-10-12T00:
source share
3 answers

This is a hint for the compiler to let you know that you are overriding the method of the parent class (or interface in Java 6).

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

This is extremely useful for quickly spotting typos or API changes. Suppose you are trying to override the method of the parent class harvest() , but conjure it with harvset() , your program will silently call the base class, and without @Override you will not have any warnings about it.

Similarly, if you use the library and in version 2 of the library, harvest() been modified to accept an integer parameter, you can no longer redefine it. Again, @Override will quickly tell you.

+122
Dec 03 2018-10-12T00:
source share

This feature is called annotation. @Override is the syntax for using annotation so that the compiler knows: β€œHey compiler, I change what harvests in the parent class,” then the compiler can immediately say, β€œDude, you called it wrong.” The compiler will not compile until you name it correctly.

So, without this @Override annotation @Override compiler would not be an error, and it would be considered a new method declaration. It would be difficult to recognize the error at this stage.

+22
Dec 31 '11 at 5:02
source share

@Override means that you are overriding the base class method. In java6, this also means that you are implementing a method from an interface. It protects you from typos when you think they override the method, but you are saddened.

+9
Dec 11 '10 at 5:29
source share



All Articles