aspose file tools*
The moose likes Servlets and the fly likes Why Servlet is an interface not an abstract class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Why Servlet is an interface not an abstract class" Watch "Why Servlet is an interface not an abstract class" New topic
Author

Why Servlet is an interface not an abstract class

Kousik Majumder
Ranch Hand

Joined: Sep 30, 2007
Posts: 229
Hi All,

Can somebody explain me why javax.servlet.Servlet is an interface NOT an abstract class?
Please help.


Thanks in Advance,
Kousik
Ravi C Kota
Ranch Hand

Joined: Jan 29, 2008
Posts: 61
Kousik Majumder wrote:Hi All,

Can somebody explain me why javax.servlet.Servlet is an interface NOT an abstract class?
Please help.


Servlet defines set of methods that can be invoked by the container. So defining the required functionalities upfront is a good approach. and if you notice, there is an abstract class called GenericServlet that implements Servlet Interface which in turn all other Servlets have been extended.

Is there any specific reason you think it should have been an abstract class rather than Servlet? As far as I understand, it is how they designed the framework. They wanted to define the functionalities first and then come up with implementation at a later point. So interface first and then implementation.


Thanks & Regards, Ravi C.Kota
SCJP 5.0, OCDJWS 5.0
Sean Clark
Rancher

Joined: Jul 15, 2009
Posts: 377

Hey,

From the javadoc:
Defines methods that all servlets must implement.
It also states:
To implement this interface, you can write a generic servlet that extends javax.servlet.GenericServlet or an HTTP servlet that extends javax.servlet.http.HttpServlet.


Sean


I love this place!
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4422
    
    8

Which is quite a common approach - have an interface that defines the behaviour (for maximum flexibility), then have an abstract class implementing that interface to provide common behaviour. See java.util.List and java.util.AbstractList for another example.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Why Servlet is an interface not an abstract class