The role of @override in Java

Source: Internet
Author: User

@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 be compiled through, because the compiler thought this method is your subclass of the method of adding itself.

Example: When overriding the OnCreate of a parent class, adding a @override system in front of the method can help you check the correctness of the method.
@Override
public void OnCreate (Bundle savedinstancestate)

{.......}
This notation is correct if you write:

@Override
public void OnCreate (Bundle savedinstancestate)
{.......}
The compiler will report the following error: The method OnCreate (Bundle) of type HelloWorld must override or implement a Supertype method, to ensure that you correctly rewrite OnCreate methods ( Because the OnCreate should be OnCreate). And if you don't add @override, the compiler will not detect an error, but will think you have defined a new method for the subclass: OnCreate

The role of @override in Java

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.