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
2010-08-05 20:56:04
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
2010-08-05 21:00:09
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
2010-08-05 21:01:38
Thank you guys!
Albus Dumbledore
2010-08-05 21:09:49
@SB: Doh, yes. Editing.
Jon Skeet
2010-08-05 21:16:36