This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Hi, 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.
Naseem, 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.