• 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
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

How to implement HAS-A relationship?

 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If class A shares HAS-A relationship with class B, is definition of instance variables the only way that these classes can share HAS-A relationship:



or can class B define variables of class A as local variable to implement HAS-A relationship?



Cheers,
Andy
 
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can implement a HAS-A relationship as long as you can create an instance of the class and this depends on what access you have to that class.

So for Class B to have a Class A, yes it would need to be part of its object state and not in a method but you still need to have access to incorporate it.

This post on encapsualtion may help with your understanding of access modifers encapsulation discussion
 
Andy James
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Kevin.

So can I infer that if class B is accessible in class A, the following is the only way to implement relationship "B HAS-A A"?



Class B can't define variable of class A as method parameter, local variable or static variable to implement relation "B HAS-A A". RIght?

Cheers,
Andy
 
Kevin Florish
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes I think my answer wasn't very clear.

So to put it another way for Class B to have a HAS-A relationship with class A then it needs to have an instance of class A as part of its object state, in other words as one of its instance variables.

Local variables only generally exist as long as the method they reside in exists on the stack, unless the local variable is an object and that object is referenced elsewhere after the method ends and is popped off the stack.

Static variables are class variables and so pertain to the class as a whole, not an instance of the class.

Method parameters are just a signature we use to pass arguments across to a method.

hth Kevin.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Andy James wrote:can class B define variables of class A as local variable to implement HAS-A relationship?


Cay Horstmann calls this relationship as *uses* relationship(Dependency) , and
he claimed that Aggregation is a stronger form of this uses relationships, since aggregation has another object as instance variable.
and also he mentioned uses relationship denoted as -----> in UML diagram . (for more details read BIG JAVA(3rd edition) - chapter:Object-Oriented Design, page-540)

hence, shall we call it as *weak* HAS-A? :-)
 
Andy James
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for all the replies. It is getting interesting. A quick question:

Does the HAS-A relationship also applies to the base classes? For example, if class A extends Base and B HAS-A A, does it also imply that class B HAS-A Base?


cheers,
Andy
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Andy James wrote:
Does the HAS-A relationship also applies to the base classes? For example, if class A extends Base and B HAS-A A, does it also imply that class B HAS-A Base?



Yup, because A IS-A Base .
 
Kevin Florish
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to expand on Seetharaman's answer a little bit.

Class B IS-A Base through inheritance not composition, so B inherits Base as it does Object through an implied extends through Base.

So B IS-A Base as opposed to HAS-A Base, whereas B HAS-A A.

But as I write this I realize that anything used from Base for A will appear in the instance variable of B, so I suppose it is also composition, I have confused myself
 
They worship nothing. They say it's because nothing is worth fighting for. Like this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic