aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Implementing 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 "Implementing HAS-A relationship" Watch "Implementing HAS-A relationship" New topic
Author

Implementing HAS-A relationship

Andy James
Ranch Hand

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

Joined: Jul 15, 2012
Posts: 31
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

Joined: Aug 22, 2005
Posts: 90
Which of the classes from B1, B2, B3 and B5 shares a HAS-A relationship with class A:



cheers
Andy
neha. bajaj
Ranch Hand

Joined: Jul 15, 2012
Posts: 31
Only B1 and B2.
Andy James
Ranch Hand

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

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Jan 28, 2008
Posts: 5575

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

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

Joined: Jul 15, 2012
Posts: 31
Andy James wrote:
I still have doubts about the correctness of the second statement though.
cheers
Andy

Andy James
Ranch Hand

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

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Aug 22, 2005
Posts: 90
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Implementing HAS-A relationship