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!