views:

356

answers:

5

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

+1  A: 

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)

thecoop
+11  A: 

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.

BalusC
+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
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
+3  A: 

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.

Tom Hawtin - tackline
+1 for the history lesson. :)
Alex Feinman
+3  A: 

The subject of this post and the body ask two different questions:

  1. Should it have at least one abstract member?
  2. 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 privateprotected constructor, not by marking it abstract.

matt b
There may be conceptual reasons why you may want an abstract class that happens to not have any abstract methods in it...
thecoop
Any example of one such scenario where you dont need an abstract method in an abstract class?
java_geek
@sai praveen, MouseAdapter is an example (if a bit specialty and highly language specific).
Yishai
@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
@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
@Yishai you're right, I should have said `protected`
matt b
@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
A: 

If a class has an abstract method it becomes abstract class (still the abstract modifier is necessary). Not the other way around.

EDIT : I guess I wasn't clear enough. A class can be made by having the tag abstract. Having an abstract method is not essential.

fastcodejava
It 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
@CPerkins - I am saying that by saying "Not the other way around".
fastcodejava
And you're still wrong. If a class has the 'abstract' modifier it is abstract by definition, whether it has abstract methods or not.
EJP
So what did 'not the other way round' mean?
EJP
@fastcodejava - Your 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