aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How to implement HAS-A relationship? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "How to implement HAS-A relationship?" Watch "How to implement HAS-A relationship?" New topic
Author

How to implement HAS-A relationship?

Andy James
Ranch Hand

Joined: Aug 22, 2005
Posts: 90
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
Kevin Florish
Ranch Hand

Joined: Jan 06, 2009
Posts: 176
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


Java5 Tutorials | Java6 Tutorials | HTML/CSS Tutorials | JavaScript and jQuery Tutorials
Andy James
Ranch Hand

Joined: Aug 22, 2005
Posts: 90
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

Joined: Jan 06, 2009
Posts: 176
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.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Aug 22, 2005
Posts: 90
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

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Jan 06, 2009
Posts: 176
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to implement HAS-A relationship?