About Question enthuware.ocpjp.v7.2.1226 :
Moderator: admin
-
- Posts: 6
- Joined: Wed May 22, 2013 4:03 am
- Contact:
About Question enthuware.ocpjp.v7.2.1226 :
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.
"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.
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1226 :
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.
2. Yes, it can be done with interfaces but the question is talking about class and subclass.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 6
- Joined: Wed May 22, 2013 4:03 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1226 :
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?admin wrote:1. If you provide a generic implementation, how will you force the subclass to provide implementation?
Maybe, I misunderstood the question.
Ok.admin wrote:2. Yes, it can be done with interfaces but the question is talking about class and subclass.
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1226 :
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.
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 6
- Joined: Wed May 22, 2013 4:03 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1226 :
Got it now.admin wrote:If you do not have abstract method in a class, you do not need an abstract class.
-
- Posts: 25
- Joined: Thu Jan 10, 2013 8:45 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1226 :
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'?You want to define common method signatures in the class but force subclasses to provide implementations for such methods.
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1226 :
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.
But again, you are technically correct.
If you like our products and services, please help us by posting your review here.
-
- Posts: 1
- Joined: Thu May 29, 2014 3:39 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1226 :
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.
-
- Posts: 6
- Joined: Sat Aug 10, 2013 1:54 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1226 :
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
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
Who is online
Users browsing this forum: No registered users and 40 guests