Is it necessary for an abstract class to have at least one abstract method?

- 188,989
- 46
- 291
- 292

- 17,585
- 30
- 91
- 113
-
1See also http://stackoverflow.com/questions/362446/abstract-class-with-all-concrete-methods, but I don't think it's *quite* a duplicate. – Michael Myers Feb 17 '10 at 18:38
5 Answers
The subject of this post and the body ask two different questions:
- Should it have at least one abstract member?
- Is it necessary to have at least one abstract member?
The answer to #2 is definitively no.
The answer to #1 is subjective and a matter of style. Personally I would say yes. If your intent is to prevent a class (with no abstract methods) from being instantiated, the best way to handle this is with a private
protected
constructor, not by marking it abstract
.

- 138,234
- 66
- 282
- 345
-
There may be conceptual reasons why you may want an abstract class that happens to not have any abstract methods in it... – thecoop Feb 17 '10 at 18:49
-
Any example of one such scenario where you dont need an abstract method in an abstract class? – java_geek Feb 17 '10 at 18:53
-
@sai praveen, MouseAdapter is an example (if a bit specialty and highly language specific). – Yishai Feb 17 '10 at 19:38
-
@Yishai, my point is that a class like this could get by with a private constructor just as well. Superclasses can then change the visibility of the constructor as need be. With that said though, I don't think this is that important of a stylistic point, and isn't exactly something I would ever be all that strict about personally. – matt b Feb 17 '10 at 20:02
-
@matt b, that is not true, subclasses cannot change the visibility of the superclass contructor. A class with only private constructors cannot be subclassed by other (non-nested or non-inner) classes. – Yishai Feb 17 '10 at 20:21
-
2@matt b, protected constructors don't give you exactly the same restrictions. For example, a class in the same package could instantiate it. But I agree that the difference is small, and for most projects unimportant. – Yishai Feb 18 '10 at 04:10
No, it is not necessary. You see this often back in "template method" design pattern, like HttpServlet
, wherein each method already has default behaviour definied and you're free to override just one (or more) of them instead of all of them.
The HttpServlet
class is merely abstract
to prevent direct initialization by inadvertendly using new HttpServlet()
which would otherwise basically have returned an empty, unimplemented and useless servlet instance.

- 1,082,665
- 372
- 3,610
- 3,555
-
+1 because this is a good point at least occasionally, though I find it a little strange that any template method pattern class could have a sane default for everything. – dsimcha Feb 17 '10 at 18:59
-
In case of `HttpServlet` it is useful, it definies to return HTTP error 503 "Method not allowed" in any of the default implemented `doXXX()` methods, fully as per the HTTP specification. – BalusC Feb 17 '10 at 19:01
In JDK 1.0 it was indeed necessary to have at least one abstract method in an abstract class. This restriction was removed in JDK 1.1 (1997? (I'm old)) and such classes added to the Java library, such as java.awt.event.KeyAdapter
.
In C++ you need at least one pure virtual function to make a subclass necessary, and at least one virtual function to add RTTI to the class. Typically it makes sense to use the destructor.
Note when overriding non-abstract methods, using @Override
is a good idea. It not only tells the reader important information about what the code is attempting to do, but also spots common errors where typos or incorrect parameter types prevents the override.

- 145,806
- 30
- 211
- 305
No - you can declare a class abstract without having any abstract methods. It may not make any sense conceptually for an instance of that class to exist, or you may want to ensure that only subclasses of that class can be instantiated (for whatever reason)

- 45,220
- 19
- 132
- 189
-
3+1 This answer the the most "to the point" and concise answer here. Thanks! – TheDevOpsGuru Sep 01 '13 at 18:01
If a class has an abstract
modifier on its declaration it becomes abstract
class.

- 305,947
- 44
- 307
- 483

- 39,895
- 28
- 133
- 186
-
2It is perfectly valid syntactically to define a class as abstract when it has no abstract methods. I'm not downvoting, but this answer is factually incorrect. Note that the language for the question is Java, not C++. – CPerkins Feb 17 '10 at 19:38
-
@CPerkins - I am saying that by saying "Not the other way around". – fastcodejava Feb 17 '10 at 20:01
-
And you're still wrong. If a class has the 'abstract' modifier it is abstract by definition, whether it has abstract methods or not. – user207421 Feb 18 '10 at 01:10
-
-
1Your second paragraph is correct, but the first one still, to my reading, even informed by the edit, is not. I just can't parse your first paragraph in a way which makes it the same as the second. My recommendation would be to delete the first paragraph entirely, and the "edit: I guess I wasn't clear enough". You'd then be clearly saying something that is correct, and not confusing other readers. Of course, the advice being unsought and free, is only guaranteed to be worth what you pay for it. – CPerkins Feb 19 '10 at 00:11