Reputation: 45
Which of the below statements indicate that we should declare a class abstract?
Select one:
a. The class is too generic to represent a real world object
b. The class has child classes that need to implement some behaviors with their own specific implementation
c. The class only has constructor, getter / setter methods without any business behaviors.
d. The class has at least one abstract method
e. Options b and d only
f. Options a, b and d
I had this question on a test and chose an incorrect answer. I believe the question is worded oddly but would like to know what the correct answer is and why.
Upvotes: 2
Views: 89
Reputation: 757
I think option e.
Reasoning:
a) I think being too generic to represent a real wold object would suggest a different problem with your OOD model, and not one that simply being abstract would solve.
b) Yes. Especially because it says, "...need to implement some behaviors...", as some of the behaviors (methods) will have implementations and others will not, that suggests abstract class, unless you factored the unimplemented behaviors out into interfaces. But in general, a key indicator of an abstract candidate is that you want to bundle some implemented behaviors with unimplemented actions (abstract methods).
c) This would make it a candidate for a POJO but not an abstract class. Being abstract is not needed for this.
d) Yes. If it has an abstract method it won't compile unless the class is declared abstract.
Upvotes: 0
Reputation: 3510
I would go with option f.
As:
a) If it isn't a real world object, you would never have to initialize it directly.
b) You could also use a non-abstract class or an interface for that, if the behaviours differ greatly. So this may be a little bit of subjective.
c) That may be the description of a POJO / JavaBean, but doesn't have to do with abstract classes.
d) If you have an abstract method you don't implement, making the class abstract is a must.
Though I must say, that this question is kind of subjective and the answer may also be e), depending on your professors opinion. But the stress on that it isn't a "real" thing in a) seems like it must be f).
Upvotes: 1