This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Implementing HAS-A relationship

 
Andy James
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
neha. bajaj
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Andy James
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which of the classes from B1, B2, B3 and B5 shares a HAS-A relationship with class A:



cheers
Andy
 
neha. bajaj
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only B1 and B2.
 
Andy James
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Andy James
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
neha. bajaj
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andy James wrote:
I still have doubts about the correctness of the second statement though.
cheers
Andy

 
Andy James
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Andy James
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic