Page 1 of 1

About Question enthuware.ocpjp.v7.2.1226 :

Posted: Mon Jun 10, 2013 5:19 am
by tduglas
Why the answer "You want to define common method signatures in the class but force subclasses to provide implementations for such methods." is correct and not "You need to provide generic implementation that can be overridden by the subclasses."?

"You want to define common method signatures in the class but force subclasses to provide implementations for such methods." - this can be done with an interface also.

Re: About Question enthuware.ocpjp.v7.2.1226 :

Posted: Mon Jun 10, 2013 6:36 am
by admin
1. If you provide a generic implementation, how will you force the subclass to provide implementation?
2. Yes, it can be done with interfaces but the question is talking about class and subclass.
HTH,
Paul.

Re: About Question enthuware.ocpjp.v7.2.1226 :

Posted: Mon Jun 10, 2013 7:48 am
by tduglas
admin wrote:1. If you provide a generic implementation, how will you force the subclass to provide implementation?
The answer tells that implementation can be overridden ("You need to provide generic implementation that can be overridden by the subclasses."), but not that you should force the override. Abstract class suite this, because one can provide generic implementation in it's methods, right?
Maybe, I misunderstood the question.
admin wrote:2. Yes, it can be done with interfaces but the question is talking about class and subclass.
Ok.

Re: About Question enthuware.ocpjp.v7.2.1226 :

Posted: Mon Jun 10, 2013 3:25 pm
by admin
The question asks, "In which of the following scenarios will you need to create an abstract class?"

The correct option says, "You want to define common method signatures in the class but force subclasses to provide implementations for such methods."

To force a subclass to provide an implementation, you need to make the method in the superclass abstract. So this is a good reason to use an abstract class.

If you do not have abstract method in a class, you do not need an abstract class. So if you provide generic implementations for the methods in a class, you don't need to make it abstract. Thus, option 2 is not correct.

HTH,
Paul.

Re: About Question enthuware.ocpjp.v7.2.1226 :

Posted: Tue Jun 11, 2013 12:36 am
by tduglas
admin wrote:If you do not have abstract method in a class, you do not need an abstract class.
Got it now.

Re: About Question enthuware.ocpjp.v7.2.1226 :

Posted: Mon Feb 10, 2014 4:50 am
by Ambiorix
You want to define common method signatures in the class but force subclasses to provide implementations for such methods.
If I also declare the subclass as abstract it's not forced to provide any implementation for the methods. Shouldn't the correct answer say 'force non-abstract subclasses' rather than just 'force subclasses'?

Re: About Question enthuware.ocpjp.v7.2.1226 :

Posted: Mon Feb 10, 2014 6:23 am
by admin
Yes, what you are saying is correct but I think you are taking it to the extreme. The question is really simple, if the objective of the superclass is to force a subclass to override a method, then the what do you do? You make the method abstract. Of course, if the subclass itself is abstract then the whole point is moot.

But again, you are technically correct.

Re: About Question enthuware.ocpjp.v7.2.1226 :

Posted: Thu May 29, 2014 3:44 pm
by smartinez
I think the benefit of abstract classes is not only that you can force subclases to implement methods (that can be done with interfaces), and not only that you can give generic implementation for some methods that can be overriden (that can be done with concrete classes), but that you can do both thing at the same time.

Re: About Question enthuware.ocpjp.v7.2.1226 :

Posted: Sun Oct 19, 2014 1:39 am
by Statics
I was confused with the question too. Exactly because "generic implementation" and "forcing subclasses to provide implementations fro some methods" are both benefits of using abstract classes and I was thinking like:
1) "You need to provide generic implementation that can be overridden by the subclasses." - yes, an abstract class does this, but a regular class can as well
2) "You want to define common method signatures in the class but force subclasses to provide implementations for such methods." - yes, an abstract class does this, but an interface can as well