File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to implement HAS-A relationship?

 
Andy James
Ranch Hand
Posts: 90
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Kevin Florish
Ranch Hand
Posts: 181
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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: 181
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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 Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • 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: 181
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic