Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
163 views
in Technique[技术] by (71.8m points)

java - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...