受保护方法允许的访问修饰符是什么

时间:2016-08-05 07:10:18

标签: java protected access-modifiers

    package servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class SampleServlet
 */
@WebServlet("/SampleServlet")
public class SampleServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public SampleServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.getWriter().append("Served at: ").append(request.getContextPath());
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);``
    }

}

在eclipse上默认生成的servlet代码中,我发现doGet和doPost方法具有受保护的访问权限。

  1. doXXX方法有哪些不同的访问修饰符?
  2. 它将如何影响班级的行为?

1 个答案:

答案 0 :(得分:1)

您不能限制继承方法的访问权限(有关详细信息,您可以检查oracle documentation)。

它不会影响servlet的行为,因为它只是来自其超类服务方法的调度方法调用(参见source code)。