• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

query

 
Ajay Kumar Rana
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
are Has-a relationships always tightly coupled ? why or why not.
 
Marimuthu Madasamy
Ranch Hand
Posts: 72
Java Monad Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hope i interpret your problem correctly,

has-a relationships are not always tightly coupled.

Ex:

1) Car has an engine //tightly coupled, since a car cannot exist without an engine.

2) Department has employees //loosely coupled
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15219
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the answer is no, but Marimuthu's example isn't right.

You can create a has-a relationship by using composition (which is the normal way to represent a has-a relationship in an object oriented program). But the member variable can be of an interface type; that way, the containing class isn't tightly coupled to the implementation of the contained class.
 
Marimuthu Madasamy
Ranch Hand
Posts: 72
Java Monad Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what Jesper said is correct.
But i interpreted it in this way.

Has-a relationships can be implemented by using both Composition and Aggregation. Here the difference is that in Composition, the lifetime of the contained object is controlled by the class which contains it.
That means in my example,

If i delete a car object, the engine object should also be deleted. (the engine alone cannot alone live without a car) (Another example, Human & heart). This is composition (Strong relationship).

But if i remove a department, employees can still live without that department. (Aggregation) (Another Example - Computer & Printer)

From Jesper's statements ("coupled to the implementation of the contained class"), I think aggregation and composition are not related to coupling.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic