Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HAS-A relationship

 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, This is from K&B mock exam
HAS-A relationships are tightly coupled.--FALSE. Why ?? Could you please give a sample example why it is False.
Given Answer is : while has-a relationships can lead to tight coupling, it is by no
means always the case.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HAS-A relationship can be achieved with the Class reference and with the Interface reference. If it's with Class reference, then tightly coupled. And, if it's with Interface, the loose coupling. So, always the HAS-A relationship aren't tightly coupled. Hence it's false.

EDITED : A typo.
 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Abimaran. Here they did not say anything about class or interface. So, why it should be tightly coupled (class implementation).
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Harikrishna Gorrepati wrote:Thanks Abimaran. Here they did not say anything about class or interface. So, why it should be tightly coupled (class implementation).

Let's ahev an example then,


Here, with the Car reference, it's tight coupling between the TestCoupling and Car. But, with the interface Bounceable reference, it's loose coupling, because, you don't need to care whether it's a Car instance or Ball instance. That's the concept of loose coupling.
 
Kushan Athukorala
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Harikrishna,

Keep in mind following definitions

1. Highly Cohesive - Classes should have well focused purpose.
2. Loosely Coupled - Degree to which one class knows the other.

Consider following real world example for the coupling with has-a relationships.

Say we have to define a House class. Think about how you can organize it.

1. House has Windows
2. House has-a Roof
3. House has-a Radio
4. House has Walls

Can you select the irrelevant relationship from above 4 items? (but possible to define in a class)

See, item 3 is not that matching with other three. If you define a House with a Radio it will be tightly coupled. But others are very well explained the House class, then the rest becomes loosely coupled.

Now you will see that following statement is correct.

"while has-a relationships can lead to tight coupling, it is by no
means always the case"

Thank you,
Kushan
 
Stephan van Hulst
Bartender
Pie
Posts: 6111
72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can decouple House even more by letting Roof and Wall have Windows, at the expense of making Roof and Wall less cohesive.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Loose coupling is a design pattern, it simply means that individual design elements should be constructed so the amount of unnecessary information they need to know about other design elements are reduced. So, my above example suits more.
 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Kushan, Stephan and Abimaran.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And have look on this image for more details about loose coupling.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic