Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can class be abstract even if does not have any abstract methods? If yes Whats the use?

I have a doubt regarding HttpServlet class is an abstract class even though there is not any abstract method in the class , all methods are concrete. Can class be abstract even if does not have any abstract methods? If yes Whats the use?

Thanks

like image 762
giri Avatar asked Jan 31 '10 12:01

giri


1 Answers

In the case of HttpServlet, the point is that servlet programmers typically don't want their servlet to support all 4 oft the main HTTP methods (POST, GET, PUT, DELETE), so it would be annoying to make the doGet(), doPost(), etc. methods abstract, since programmers would be forced to implement methods that they don't need. Therefore, HttpServlet provides a default implementation for all those methods that does nothing except return an error statuscode to the client. Programmers can override the methods they need and not worry about the rest. But actually using the HttpServlet class itself make no sense (since it does nothing useful), so it's abstract.

And there you have a great example for when it can make sense to have an abstract class without any abstract method.

like image 74
Michael Borgwardt Avatar answered Sep 24 '22 10:09

Michael Borgwardt