Two Laptop Bag
The moose likes Servlets and the fly likes abstract HttpServlet? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "abstract HttpServlet?" Watch "abstract HttpServlet?" New topic

abstract HttpServlet?

Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
Can anyone please explain me why the HttpServlet class defined in javax.servlet.http package is an abstract class. GenericServlet is an abstract class as it implements Servlet interface and thus inherits init(ServletConfig) method from it. Since it does not override init(Config) method and provide its body so it has to be an abstract class. GenericServlet class overloads init method and it also defines its body.

HttpServlet does not directly implement Servlet interface. All the methods which it inherits from GenericServlet is not abstract. So it must be a concrete class.

The only reason which I can think of is just to prevent direct instantiation of HttpServlet. Is there any other reason behind it.

Thanks & Regards

[ July 08, 2006: Message edited by: Naseem Khan ]

Asking Smart Questions FAQ - How To Put Your Code In Code Tags
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33130

It wouldn't make sense to directly instantiate a HttpServlet class as it wouldn't do anything.

One of the doXXX methods needs to be implemented to provide functionality. The reason the class isn't abstract is so you aren't forced to implement all the doXXX methods. Basically, it provides dummy implementations for you.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
Thanks Jeanne.

I got your point.


I agree. Here's the link:
subject: abstract HttpServlet?
It's not a secret anymore!