• Post Reply Bookmark Topic Watch Topic
  • New Topic

Abstract class with no abstract method. What is the use?  RSS feed

 
Ban Mido
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

One of my friend came across this question in an interview.

Can you have an abstract class without any abstract method? -> Yes
Then what is the use of such class where no methods are abstract but the class alone is abstract? - Can anyone answer this?

Thanks..
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ban Mido wrote:Then what is the use of such class where no methods are abstract but the class alone is abstract? - Can anyone answer this?

The main one I can think of is a skeleton implementation - that is: a fully operational implementation where developers are free (and possibly even encouraged) to override the "default" implementation with their own. Although it has to be said that an extendable concrete class would also work for such a case.

I'm afraid I can't think of a mainstream example off the top of my head (although AbstractList comes quite close); perhaps someone else can.

Winston

Edit: Another example - although it's a bit of a cheat - is an abstract class that extends an existing one that does have abstract methods. AbstractSet is a specific example of such a class.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are also cases where the subclass is supposed to implement/override one of a number of methods. But it doesn't have to override all of them, so they can't be made abstract.

A couple of examples:

- MouseAdapter. This is an empty implementation of MouseListener. If you're only interested in one event it saves you having to provide blank implementations of the others. But since it does precisely nothing there's never a reason to use a MouseAdapter directly, so it's made abstract.

- HttpServlet. Here the subclass needs to implement one or more of the doXXX methods (doPost, doGet etc). But again, it should only implement the ones it's interested in. The others will have the right default behaviour (throw an exception if called).
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about the Listeners in the java.awt.event package? Many are accompanied by “adapter” classes. They are archetypal skeleton implementations.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just a possibility - by having all its non-abstract methods as static, it can force itself to be used as a utility class and an instance can never be created for it.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In general, though, it can just be meant as "there is no reason to instantiate this class - for inheritance only".

A specific example: the code I'm working on right now (or rather, should be, rather than typing this ) has some Entity base classes that are used as base classes for all the domain objects. They don't have any abstract methods, but they have no real use until a specific domain object extends one and adds some properties. So they're marked as abstract - it makes it clear they aren't meant to be used as-is.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Jai wrote:Just a possibility - by having all its non-abstract methods as static, it can force itself to be used as a utility class and an instance can never be created for it.

Ooof. Not the best use, I'd say. There are other (and better) ways to make a class uninstantiable.

Winston
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, and welcome to the Ranch!
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd like to add one more use case: by creating an abstract class that doesn't have a default constructor, you compel extending classes to obtain and pass specific parameters in a super(...) call.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl Burke wrote:I'd like to add one more use case: by creating an abstract class that doesn't have a default constructor, you compel extending classes to obtain and pass specific parameters in a super(...) call.

Ah yes. Good point.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!