File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Abstract classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Abstract classes" Watch "Abstract classes" New topic

Abstract classes

Karan Rajan

Joined: May 19, 2005
Posts: 12
My understanding of the purpose of abstract classes is that the abstract classes let you define some behaviors; they force your subclasses to provide others.

Now, an abstract class need not have any abstract methods. So if we do have an abstract class without any abstract methods then we will not be forcing a subclass to override any method.

So why would we want to have an abstract class without any abstract methods ?
I noticed this as I was doing some reading on HttpServlet class. The HttpServlet class is an abstract class without any of its methods being abstract.

Many thanks.
Kamal Ahmed
Ranch Hand

Joined: Feb 15, 2005
Posts: 91
It�s possible to declare a class as abstract without including any abstract methods. This is useful when you�ve got a class in which it doesn�t make sense to have any abstract methods, and yet you want to prevent any instances of that class.
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
If the class were not abstract, then a developer may be tempted to use it as is. But in the case of HttpServlet, at least one of its methods must be overridden (usually doGet() or doPost()). So, my guess is that Sun is forcing a developer to subclass HttpServlet who, in doing so, is then more likely to do the right thing and override the appropriate method(s).

SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
There are some abstract DefaultWhatever classes in Swing. They implement all the methods required by the Whatever interface, but all the methods do nothing. You can extend this class to override any methods you need with less effort than implementing all the methods in the interface. It feels like cheating somehow, but it's very handy.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199


Welcome to JavaRanch!

A bit of business: you may not have read our naming policy on the way in. It requires that you use a full, real (sounding) first and last name for your display name. Initials aren't enough. You can change your display name here. Thanks!

[Jess in Action][AskingGoodQuestions]
I agree. Here's the link:
subject: Abstract classes
It's not a secret anymore!