File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Why generic servlet is an abstract class? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Why generic servlet is an abstract class?" Watch "Why generic servlet is an abstract class?" New topic

Why generic servlet is an abstract class?

rahul S Sharma

Joined: Jun 08, 2011
Posts: 21
According to my knowledge, Generic Servlet class implements Servlet interface and it gives definition of all the methods present in Servlet interface except service() method. service() method is abstract in Generic Servlet class, that's why Generic Servlet is an abstract class.Please correct me if I am wrong or is there anything I can add in my explanation.
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63844

Yes, this is basic Java. If any method is abstract, then the class must be abstract.

[Asking smart questions] [About Bear] [Books by Bear]
rahul S Sharma

Joined: Jun 08, 2011
Posts: 21
What about HttpServlet class? This class doesn't have any abstract method, still is an abstract class.
Arun Giridharan
Ranch Hand

Joined: Sep 30, 2010
Posts: 290

yes you can make , even though the class doesn't have any abstract methods , for me the main reason i could say is doPost() or doGet() or other HTTP method than this , are just by-passing means it doesn't have any logical code in it . (This may be the main reason)
{Is it correct Bear Bibeault .}

Dieter Quickfend

Joined: Aug 06, 2010
Posts: 543

HttpServlet is abstract because they don't want you instantiating it without subclassing it. Pretty much because there is no use at all. Java does a lot to protect developers from their own stupidity (checked exceptions, strong typing, ...), why shouldn't they do that here? :P

Oracle Certified Professional: Java SE 6 Programmer && Oracle Certified Expert: (JEE 6 Web Component Developer && JEE 6 EJB Developer)
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

GenericServlet IS-A Servlet . and this GenericServlet has some common functionality/method to the Servlet class. As of Now, there is a HttpServlet which is specific to the Http protocal extends GenericServlet and uses some servlet methods. in future, pretend there is a FTPServlet which may be extends GenericServlet.
Matthew Brown

Joined: Apr 06, 2010
Posts: 4543

The think about HttpServlet is that while it's completely useless without overriding one of the service methods (hence making it abstract), it would be a pain to have to implement all the service methods if all you wanted was something to respond to GET requests. So that's why those methods aren't abstract.
I agree. Here's the link:
subject: Why generic servlet is an abstract class?
It's not a secret anymore!