aspose file tools
The moose likes Beginning Java and the fly likes Abstract class with no abstract method. What is the use? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Abstract class with no abstract method. What is the use?" Watch "Abstract class with no abstract method. What is the use?" New topic
Author

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

Ban Mido
Greenhorn

Joined: Dec 02, 2009
Posts: 1
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

Joined: Mar 17, 2011
Posts: 4761
    
    7

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.


Isn't it funny how there's always time and money enough to do it WRONG?
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 3795
    
    1

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
Sheriff

Joined: Oct 13, 2005
Posts: 32708
    
    4
What about the Listeners in the java.awt.event package? Many are accompanied by “adapter” classes. They are archetypal skeleton implementations.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1778
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

Joined: Apr 06, 2010
Posts: 3795
    
    1

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

Joined: Mar 17, 2011
Posts: 4761
    
    7

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

Joined: Apr 06, 2010
Posts: 3795
    
    1

Oh, and welcome to the Ranch!
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4167
    
    3

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.


luck, db
There are no new questions, but there may be new answers.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 4761
    
    7

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
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Abstract class with no abstract method. What is the use?
 
Similar Threads
Abstract
abstract
ABSTRACT
Abstract class
use of abstract with interface