Forums Register Login

Implementing HAS-A relationship

+Pie Number of slices to send: Send
Can I implement HAS-A relationship by using only instance variables? Or if a class uses a variable of another class, in any scope, it can be stated to be associated with it.

cheers
Andy
+Pie Number of slices to send: Send
 

Andy James wrote:Can I implement HAS-A relationship by using only instance variables? Or if a class uses a variable of another class, in any scope, it can be stated to be associated with it.

cheers
Andy



When a class has a reference variable of another class then we can say HAS-A relationship.If you can implement HAS-A relationship through instance variables then please write that code here.
+Pie Number of slices to send: Send
Which of the classes from B1, B2, B3 and B5 shares a HAS-A relationship with class A:



cheers
Andy
+Pie Number of slices to send: Send
Only B1 and B2.
+Pie Number of slices to send: Send
 

neha. bajaj wrote:Only B1 and B2.



Can you please explain why B3 and B5 aren't 'associated' with A, even though B3 and B5 can manipulate an object of class A?

cheers
Andy
+Pie Number of slices to send: Send
 

Andy James wrote:Can I implement HAS-A relationship by using only instance variables?


applied to class variable also

Andy James wrote:
if a class uses a variable of another class, in any scope, it can be stated to be associated with it.


Association wont apply to local variables
+Pie Number of slices to send: Send
 

Andy James wrote:
Can you please explain why B3 and B5 aren't 'associated' with A, even though B3 and B5 can manipulate an object of class A?


because only method uses A. it is not a HAS-A relationship.
+Pie Number of slices to send: Send
 

Seetharaman Venkatasamy wrote:

Andy James wrote:
Can you please explain why B3 and B5 aren't 'associated' with A, even though B3 and B5 can manipulate an object of class A?


because only method uses A. it is not a HAS-A relationship.



Thanks for answering my query.

So are we stating -
Class B can share a HAS-A relation with class A only if class B defines an instance or class variable of type A.
Class A is NOT associated with class B, if a method defined in class B uses class A.

I still have doubts about the correctness of the second statement though.

cheers
Andy
+Pie Number of slices to send: Send
 

Andy James wrote:
I still have doubts about the correctness of the second statement though.
cheers
Andy


+Pie Number of slices to send: Send
As I stated, I don not agree with - HAS-A relationship cannot be implemented using local variables or method parameters.


I support of my statement (source:http://www.ootips.org/uml-hasa.html):

Problem: UML has several relations (association, aggregation and composition) that seem to all mean the same thing : "has a".
So, what is the difference between them?

Robert C. Martin explained:
Association represents the ability of one instance to send a message to another instance. This is typically implemented with a pointer or reference instance variable, although it might also be implemented as a method argument, or the creation of a local variable.

Any arguments?

cheers
Andy
+Pie Number of slices to send: Send
 

Andy James wrote:
Robert C. Martin explained:
Association represents the ability of one instance to send a message to another instance. This is typically implemented with a pointer or reference instance variable, although it might also be implemented as a method argument, or the creation of a local variable.


what happen if the particular method is not invoked on an Object?
for example A obj = new A(); is that Object Has-A B?
+Pie Number of slices to send: Send
 

Seetharaman Venkatasamy wrote:

Andy James wrote:
Robert C. Martin explained:
Association represents the ability of one instance to send a message to another instance. This is typically implemented with a pointer or reference instance variable, although it might also be implemented as a method argument, or the creation of a local variable.


what happen if the particular method is not invoked on an Object?
for example A obj = new A(); is that Object Has-A B?




What happens if an instance variable or a class variable is not initialized with an object?

HAS-A relationship is an ability of one instance to send a message to another instance.

cheers
Andy
Evildoers! Eat my justice! And this tiny ad's justice too!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1178 times.
Similar Threads
question about "has a" at SUN's mock exam
sample question on Sun website
K&B question
HAS-A relationship problem
question from sun
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 09:27:21.