Home >Java >javaTutorial >How to use @Override annotation in java

How to use @Override annotation in java

PHPz
PHPzforward
2023-05-04 10:16:061431browse

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 {
}

Example

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

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!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete