| Author |
About Has-a relationship
|
Abimaran Kugathasan
Ranch Hand
Joined: Nov 04, 2009
Posts: 2066
|
|
Are these correct?
1) Has-a relationship should never be encapsulated.
2) Has-a relationships can be implemented using instance variables.
3) An array or a collection can be used to implement a one-to-many Has-a relationship.
What about the reasons?
1) 1st one has no sense. Because we can encapsulate data only, not relationship.. OK?
2) What about 2nd..? It seems to be correct, But instance variables???
3) It's correct???
Please confirm??? Thanks in Advanced!
|
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
|
 |
Wouter Oet
Saloon Keeper
Joined: Oct 25, 2008
Posts: 2700
|
|
1) Well the data is the relationship and it should be encapsulated so that you're programming to an interface rather then direct instance variables.
2) Correct
3) Correct although it would be a zero-to-many relationship
|
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
|
 |
Abimaran Kugathasan
Ranch Hand
Joined: Nov 04, 2009
Posts: 2066
|
|
Thanks a lot Wouter Oet..
In second question, is it instance variable or instance of another class?
|
 |
Wouter Oet
Saloon Keeper
Joined: Oct 25, 2008
Posts: 2700
|
|
I don't understand your question but I think that you're not sure what an instance variable is.
Example
You can read about it here
|
 |
Abimaran Kugathasan
Ranch Hand
Joined: Nov 04, 2009
Posts: 2066
|
|
Sorry, I confused there.... Instance variable also can be an instance of other classes(Ex : String)....
Again Thanks Wouter Oet........ It's perfectly correct....
|
 |
Nidhi Sar
Ranch Hand
Joined: Oct 19, 2009
Posts: 252
|
|
Wouter Oet wrote:
...
3) An array or a collection can be used to implement a one-to-many Has-a relationship.
...
3) Correct although it would be a zero-to-many relationship
Hey Wouter!
Just wondering why you wrote that! It is a one-to-many relationship right? e.g. consider this:
The instance variable la implements (one B) - to - (many A).
Am I wrong??
|
"A problem well stated is a problem half solved.” - Charles F. Kettering
SCJP 6, OCPJWCD
|
 |
Wouter Oet
Saloon Keeper
Joined: Oct 25, 2008
Posts: 2700
|
|
You've made me think a lot harder about this. I guess that it depends on what one stands for. If it's referring to the number of Objects in the collection/array then it's zero-to-many (that is what I thought) because the collection/array can be empty. However if it's referring to the relation then it's one. But that is also debatable because A doesn't have a relation with B except for that it's in a collection/array in B. In that case it depends on if the relation is unidirectional or bidirectional.
|
 |
Neha Daga
Ranch Hand
Joined: Oct 30, 2009
Posts: 504
|
|
here its the relation between class and the instance variables.
and the list in example signifies that one instance of class B has many instances of class A. so, one to many has-a relationship.
|
SCJP 1.6 96%
|
 |
Abimaran Kugathasan
Ranch Hand
Joined: Nov 04, 2009
Posts: 2066
|
|
|
Thanks to all. Then what about zero-to-many? I couldn't understand that point. Nidhi Sar, What is that?
|
 |
Nidhi Sar
Ranch Hand
Joined: Oct 19, 2009
Posts: 252
|
|
Hi Abiraman,
It was Wouter actually who brought up zero-to-one. But from his previous explanation, I can see that it depends on the way you look at it...
|
 |
Vivek K Singh
Ranch Hand
Joined: Dec 22, 2009
Posts: 85
|
|
|
you can look at it like One B HAS zero to many A
|
SCJP 6
|
 |
 |
|
|
subject: About Has-a relationship
|
|
|