• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

HAS-A relationship

 
Greenhorn
Posts: 12
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

Imagine that you have these classes:



My question is: can we say that Car HAS-A Component?

Thanks.
 
Ranch Hand
Posts: 43
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. The car has a component in form of Engine.
 
Gesu Saguzo
Greenhorn
Posts: 12
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Amar.

Then, why the OCP Java SE 7 Programmer II Certification Guide in Q 3-3 states that for this code:



The statement "Sun HAS-A AbX" is incorrect because "Class Sun defines a variable of type Sunny. So the correct relation would be Sun HAS-A Sunny. The IS-A and HAS-A relationships don’t reflect the names of the variables"?.

Then, is there an error in the answer?

Thanks.
 
Ranch Hand
Posts: 472
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try look to variable type and variable name
 
Gesu Saguzo
Greenhorn
Posts: 12
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Sergej,

- The variable type is "Sunny" which is equivalent to "Engine" in the first example.

- The variable name does not matter because it is just a name.

So, is the answer wrong?

Thanks,
Jesús.
 
Sergej Smoljanov
Ranch Hand
Posts: 472
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sun has AbX type variable inside?
Has a is about type, not about name.
 
Gesu Saguzo
Greenhorn
Posts: 12
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok Sergey,

So, for you in the first example, the statatement "Car HAS-A Component" is not valid, right?

Thanks.
 
Sergej Smoljanov
Ranch Hand
Posts: 472
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Car HAS-A Component" is not valid.
My consideration in code:

may be some of more experienced ranchers say, right or wrong i am?

also you may be interesting in question static variable participant of has a:
https://coderanch.com/t/654347/java-programmer-OCPJP/certification/related-Mala-Gupta-Programmer
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's a strange question I would say. As Sergej said, it's trying to confuse you here

Sunny AbX;

Here the variable name is the same as the super class of Sunny. Tricky question but at the same time confusing IMO.

I agree regarding Engine and Component example also. Car has-a Engine, but can I say Car has-a Component? It's not like I can assign any Component to the engine reference, it has to be an Engine.

Engine engine = new Tyre(); // doesn't compile

In real life it makes sense which is what OOP is modeled after so it should be true that Car has-a Component. But Car has-a specialized type of Component so not sure if we can say Car has-a Component...
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this:

 
It's weird that we cook bacon and bake cookies. Eat this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic