File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why Servlet is an interface not an abstract class

 
Kousik Majumder
Ranch Hand
Posts: 237
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Can somebody explain me why javax.servlet.Servlet is an interface NOT an abstract class?
Please help.
 
Ravi C Kota
Ranch Hand
Posts: 61
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sean Clark
Rancher
Posts: 377
Android Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Matthew Brown
Bartender
Posts: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic