javax.servlet.Servlet
is an interface which is implemented by javax.servlet.http.HttpServlet
. That means the methods of Servlet
are never invoked; an interface just defines an API - there is no implementation behind it.
The servlet life cycle will make sure that first the init()
method of HttpServlet
will be called. When requests come in that match the servlet's URL, the container will call HttpServlet.service()
which distinguished between the various HTTP types (GET
, POST
, ...) and call the correct handler method (doGet()
, doPost()
, ...).
[EDIT] You should read up on the difference between Java interfaces and classes.
Maybe it's easier to understand when you see some code:
Servlet servlet = new HttpServlet(...);
...
servlet.init();
...
servlet.service(...);
The ...
means "something happens here but it's not important to understand the example".
A real servlet is created by extending HttpServlet
. This class implements the Servlet
interface. That means the assignment works. The compiler will use the methods defined in Servlet
to find out whether servlet.init()
is valid. But at runtime, the method HttpServlet.init()
will be called.