views:

104

answers:

1

I wonder what is the difference in Java between an abstract class and a class that has all its methods abstract? I mean, is an abstract class just a class whose methods automatically get abstract?

+11  A: 

Absolutely not. Indeed, a class can be abstract without any methods being abstract, although that's relatively rare (see Mark's comment below for an example). On the other hand, if a class has any abstract methods, then it must be declared abstract.

Generally speaking, the purpose of an abstract class is to provide a skeleton with some non-abstract behaviour, but other bits still to be filled in by subclasses. This can be used with the template method pattern, for example.

Jon Skeet
Do you mean if a class has any abstract methods (not if a method has any abstract methods)? I'm not about to edit a response from you without being sure..
SB
Some of the only valid uses of an abstract class with no abstract methods I've seen are the GUI adapter classes in Swing like MouseAdapter: http://download.oracle.com/javase/6/docs/api/java/awt/event/MouseAdapter.html. The purpose of the class is to provide default no-op implementations (but still implemented!) of a bunch of methods to reduce boilerplate, yet it makes no sense to instantiate the class as-is.
Mark Peters
Thank you guys!
Albus Dumbledore
@SB: Doh, yes. Editing.
Jon Skeet