whether in NetBeans or in the Eclipse development environment, when writing Java code often encounter @override, then @override specific what is it?
@Override is pseudo-code, which means rewriting (and of course not writing), but the following benefits are written:
1, can be used as a comment, easy to read
2, the compiler can give you to verify @override the following method name is all in your parent class, if there is no error
For example, if you do not write @override and your next method name is wrong, then your compiler can pass (it thought this method is your subclass in the addition of its own method).
As shown in the following example:
Class Fu{private int a = 1;void print () {System.out.println ("parent Class");}} Class Zi extends fu{@Overridevoid prin ()//Sub-class method name differs from parent class, compile error {System.out.println ("subclass");}} public class Test{public static void Main (string[] args) {new Zi (). Prin ();}}
Therefore, when we hand-copy the method of the parent class, it is easy to mistake the method parameter or the method name, if not @override at this time, the editor will not prompt.
The following quotations:
Share a little secret behind the @override tag---record Java's thought-line oneness
Bruce, author of Thinking in Java, discusses this issue by mentioning an example of the override private approach:
Now we add a private method to the Fu class, and try to replicate in the Zi
The compiler will prompt for an error, which is a very low-level error, but sometimes it's just not something we find out: it's trying to replicate a private method, but when we remove the override tag, the compiler doesn't give an error and it can be executed.
In fact, your so-called carbon print in Zi is just a private method for the Zi itself. is a completely new approach.
This begs the question, what is a carbon copy?
Private method can not make a copy of , but think again, just understand:
This is the object-oriented design of the original intention, the private method itself is to encapsulate within the class, do not want others to change or external references, see here, suddenly feel, Java design is really good, feel the idea and the realization of unity.
Previously always felt that the override tag is dispensable, but did not expect to elicit so many problems, so get a revelation: seriously think about the meaning of every grammatical detail, think line unity.
What's @Override?