my dog learned polymorphism
The moose likes Beginning Java and the fly likes query Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "query" Watch "query" New topic


Ajay Kumar Rana

Joined: Feb 27, 2008
Posts: 13
are Has-a relationships always tightly coupled ? why or why not.
Marimuthu Madasamy
Ranch Hand

Joined: Jun 07, 2007
Posts: 72

Hope i interpret your problem correctly,

has-a relationships are not always tightly coupled.


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

2) Department has employees //loosely coupled

- Marimuthu Madasamy
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15081

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.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Marimuthu Madasamy
Ranch Hand

Joined: Jun 07, 2007
Posts: 72

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.
I agree. Here's the link:
subject: query
It's not a secret anymore!