About Question enthuware.ocpjp.v7.2.1082 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
muttley
Posts: 19
Joined: Thu Feb 28, 2013 9:47 am
Contact:

About Question enthuware.ocpjp.v7.2.1082 :

Post by muttley »

Sorry but I don't understand the affirmation.
When you are trying to reuse functionality from multiple classes and your class already extends from a framework class.
In the composition we don't need nor extends other classes neither a framework class.
:?
Therefore the answer is wrong because assert that a "class already extends from a framework class".

admin
Site Admin
Posts: 10065
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

That is exactly why composition is suitable here. The option is giving you a situation and asking you whether composition will be helpful in that situation.

For example, if your class already extends from a class A, and if you want to reuse the functionality from another class B (or more), you can't extend from B now (because you are already extending from A). Thus, you will use composition here.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

Ambiorix
Posts: 25
Joined: Thu Jan 10, 2013 8:45 am
Contact:

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

Post by Ambiorix »

With regards to the comment below option 1, there is actually a Composite interface in java.awt

http://docs.oracle.com/javase/7/docs/ap ... osite.html

EpicWestern
Posts: 17
Joined: Wed Jan 22, 2014 12:35 pm
Contact:

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

Post by EpicWestern »

Composition:

class Car {   public accelerate(){ } }
class SportsCar {  
   Car c;    
public accelerate() {        //delegate the call to Car     
    c.accelerate();     }
}

I think you mean "Car" to be engine or something similar. Otherwise its a very poor example of composition because a Sports Car does not "have a" car.

admin
Site Admin
Posts: 10065
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

You should not look at the above example in isolation. Right above this example, there is another example that shows the inheritance approach for the same classes. This example is only meant to illustrate that technically, it may be possible to achieve reuse using either of the approaches. However, which approach is more more appropriate depends on the situation. In this situation, as you rightly pointed out, composition looks absurd. That is the point.
If you like our products and services, please help us by posting your review here.

EpicWestern
Posts: 17
Joined: Wed Jan 22, 2014 12:35 pm
Contact:

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

Post by EpicWestern »

The question is geared toward defining composition as an approach to modeling classes (as opposed to inheritance), but I think the general definition of the term is just the "has-a" relationship between classes.

The following is an example of composition:
class A{}
class B {
A a;
}

So when a composition relationship exists in so many different types of classes, all of the answers are potentially correct.

admin
Site Admin
Posts: 10065
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

So when a composition relationship exists in so many different types of classes, all of the answers are potentially correct.
I am not sure how you drew that conclusion. Yes, has-a is basically composition but that still doesn't make other choices valid.
If you like our products and services, please help us by posting your review here.

EpicWestern
Posts: 17
Joined: Wed Jan 22, 2014 12:35 pm
Contact:

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

Post by EpicWestern »

admin wrote: I am not sure how you drew that conclusion. Yes, has-a is basically composition but that still doesn't make other choices valid.
The question is:
"In which of the following situations will you use composition?"

Coming up with an example of where any of the choices use a "has-a" relationship is trivially easy.

When your class has a public no arg constructor:
public class A {
B b;
public A() {}
}
class B {}

Its not like I would even be doing anything weird with the code. Any of the classes in the choices using composition in the general sense isn't just possible, its perfectly normal.

admin
Site Admin
Posts: 10065
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

Let me put it another way - Why would you use composition in the following cases?
1. When your class does not extend any other class and is thus free to implement Composite interface.
- This doesn't even make sense.

2. When you are trying to reuse functionality from an existing class by extending from that class.
- If you are trying to reuse functionality of class A in class B by extending, you would do B extends A. Where do you see composition here?

3. When your class has a public no args constructor.
- What has this got to do with composition?? Will you use composition every single time your class has a no args constructor? I hope not. Then clearly, this is not a reason to use composition.

The question is not talking about whether you can use composition for something unrelated. Of course, you can write a class that does 10 different things and one of them could be composition. The question is giving you a situation (or a problem, if you will)and asking you if you will use composition to solve that problem.

I have no idea how you think these options are valid.
If you like our products and services, please help us by posting your review here.

EpicWestern
Posts: 17
Joined: Wed Jan 22, 2014 12:35 pm
Contact:

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

Post by EpicWestern »

admin wrote: The question is not talking about whether you can use composition for something unrelated.
It doesn't rule that out which is the problem. Since we both agree all the classes in your examples both can and commonly use composition, all of them are valid answers to your question as printed.
The question is giving you a situation (or a problem, if you will)and asking you if you will use composition to solve that problem.
No its not. Two of the possible answers are just types of classes, there's no question or problem described. So does that type of class use composition? Why wouldn't it?

I mean i can understand your intention for the question and how all the answers might not fit your intention, but for your question as its written all of the answers are valid.

admin
Site Admin
Posts: 10065
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

The question says, "In which of the following situations will you use composition?"

If the question were something like this, "In which of the following cases will you buy a ticket for a balloon ride", will you still say all the options are valid? Because by your logic, you can write a class with a private constructor, and still buy the ticket!
If you like our products and services, please help us by posting your review here.

pfilaretov
Posts: 35
Joined: Mon Jul 28, 2014 2:05 am
Contact:

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

Post by pfilaretov »

What is the difference between Composition and Delegation?

admin
Site Admin
Posts: 10065
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

If you like our products and services, please help us by posting your review here.

jagoneye
Posts: 97
Joined: Wed Dec 28, 2016 9:00 am
Contact:

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

Post by jagoneye »

Does composition lead to high coupling since you are dependent on the object used to delegate the call?

admin
Site Admin
Posts: 10065
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

It does increase coupling but not necessarily to a high level. Depends on how the dependent class is being used by the owner. If the owner is exposed to the inner details of the dependent class, then it is a problem.
Here is a good discussion: http://stackoverflow.com/questions/3085 ... n-coupling
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 232 guests