What's @Override?

Source: Internet
Author: User

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?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.