• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Has-a relationship

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In K&B book,P158, self test 1):
A Has-a relationships always rely on inheritance.
B Has-a relationships always rely on instance variables.
C Has-a relationships always require at least two class type.
D Has-a relations always rely on polymorphism.
E Has-a relationship are always tightly coupled.

The given answer is B and I can tell others are obviously wrong.
However, In K&B bonus master exam, we have the following question:

A Has-a relationships are, by definition, well encapsulated.
B A covariant return requires the existence of a has-a relationship.
C Overriding requires the existence of a is-a relationship.
D In some cases, is-a relationships are used in the process of autoboxing.
E Has-a relationships are always defined with instance variables.

The given answer is C&&D. According to the self test question, I think E is also the right answer. The explanation is that class (static) variables can also be used to define has-a relationships.

Therefore, I think these two question are contradictory to one another. Could anyone help me clear this?
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A HAS-A relationship can also be accomplished by a static variable right?
 
Yeming Hu
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Paul van den Berg:
A HAS-A relationship can also be accomplished by a static variable right?



It seems so, so what about the first question? There should be no answer for it
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Yeming Hu:
... Therefore, I think these two question are contradictory to one another...


I agree. I don't see p. 158 listed in the K&B Errata page, but my understanding is that static fields constitute a HAS-A relationship.
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well I agree with you guys but it raises a different question in my mind... which one of the answers are wrong?

Notice that solution E states:
Has-a relationships are always defined with instance variables.

Can a "Has-a" relationships be defined with a static variable?

If class Animal has a static variable of type Collar, and class Dog extends Animal, wouldn't class Dog "has-a" Collar too?

I'm confused regarding the working here... is a static variable considered an instance variable? I don't think so... sun reference but now i'm not quite sure....
 
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well!!may i be am not correct but

what i think is since static variables and methods are not attached to the instance of class using class object we cannot access static methods or variables......but using the class name we can access static methods or variables may be bcoz of this it is mentioned,HAS a relationship can be defined using static variables of class.

Although
what actualy is that
HAS-a relationship is defined using instance variables of class.....i too thought E also should be the answer.......

i hope it helps........
 
I'm thinking about a new battle cry. Maybe "Not in the face! Not in the face!" Any thoughts tiny ad?
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic