• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

About Has-a relationship

 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Wouter Oet..

In second question, is it instance variable or instance of another class?
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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??
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all. Then what about zero-to-many? I couldn't understand that point. Nidhi Sar, What is that?
 
Nidhi Sar
Ranch Hand
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 85
Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can look at it like One B HAS zero to many A
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic