Home > Java > javaTutorial > body text

How to use @Override annotation in java

PHPz
Release: 2023-05-04 10:16:06
forward
1400 people have browsed it

Explanation

1. It does not have any attributes, so it cannot store any other information. It can only be used in methods and is discarded after compilation.

2. This is a typical tagged annotation, which only the compiler knows.

In the process of compiling java files into bytecode, once the compiler finds that a method has been modified with annotations, it will match whether there is a function with the same method signature in the parent class. If not, naturally It cannot pass compilation.

Definition

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface Override {
}
Copy after login

Example

public class AdminServlet extends HttpServlet{
    @Override  //表示方法重写
    protected void service(HttpServletRequest req,HttpServletResponse resp)
    throws ServletException,IOException{
        req.setCharacterEncoding("utf-8");
    }
}
Copy after login

The above is the detailed content of How to use @Override annotation in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!