Home  >  Article  >  Java  >  Detailed explanation of the role of @Override in Java

Detailed explanation of the role of @Override in Java

黄舟
黄舟Original
2017-10-17 10:12:001667browse

This article mainly introduces relevant information that explains the role of @Override in Java. I hope this article can help everyone understand this part of the content. Friends in need can refer to it

Detailed explanation of the role of @Override in Java

@Override is pseudo code, which means rewriting (of course you can not write it), but writing it has the following benefits:

1 , can be used as comments for easy reading;

2. The compiler can verify for you whether the method name under @Override is owned by your parent class, and if not, an error will be reported. For example, if you do not write @Override and you write the wrong method name below, your compiler can compile it because the compiler thinks that this method is a method added by yourself in your subclass.

Example: When rewriting the onCreate of the parent class, adding @Override in front of the method can help you check the correctness of the method.


@Override
public void onCreate(Bundle savedInstanceState)
{…….}

This way of writing is correct, if you write:


@Override
public void onCreate(Bundle savedInstanceState)
{…….}

compile The device will report the following error: The method oncreate(Bundle) of type HelloWorld must override or implement a supertype method to ensure that you override the onCreate method correctly (because oncreate should be onCreate). And if you do not add @Override, the compiler will not detect an error, but will think that you have defined a new method for the subclass: oncreate

The above is the detailed content of Detailed explanation of the role of @Override in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn