Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why HttpServlet class is declared as abstract? [closed]

Tags:

java

servlets

Why HttpServlet class is declared as abstract even there is no abstract method in that class?

like image 826
satheesh Avatar asked Jul 19 '11 05:07

satheesh


2 Answers

It's because it follows the Template Method design pattern. The doXxx() methods have all default behaviours of returning a HTTP 405 Method Not Implemented error. If those methods were all abstract, you would be forced to override them all, even though your business requirements don't need it at all. It would only result in boilerplate code and unspecified/unintuitive behaviour.

like image 136
BalusC Avatar answered Nov 16 '22 04:11

BalusC


It is declared as a class instead of an interface to implement most of the cruft/repeatable code required for setting up a servlet. It is declared as abstract since it wouldn't make sense to instantiate a "bare bones" servlet which takes care only of the setup and doesn't contain any custom/user defined logic in it.

like image 21
Sanjay T. Sharma Avatar answered Nov 16 '22 03:11

Sanjay T. Sharma