This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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?
Joined: Jan 06, 2009
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.
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? :-)
Joined: Aug 22, 2005
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?