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

Coupling question

 
Adam Jordan
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

Just a quick one about Coupling:

Would a class that contains objects of other classes be considered 'bad' or 'highly' coupled?

Eg.



I understand that these are coupled, but it won't matter if the internal representation of an Engine changes as long as the class remains of type Engine.

*Confused* :-)
 
Harsha Smith
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Would a class that contains objects of other classes be considered 'bad' or 'highly' coupled?


Coupling to some extent is necessary. In a meaningful application, an Object shouldn't be expected to do everything on its own.

Your example shows that the objects are coupled. The internal representation of the Engine can be changed as along as it doesn't effect the AirCraft or in any class where Engine objects are used.
 
Nomaan Butt
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
coupling should not be an issue, it is required a lot number of time. References of other class in a class is quite necessary. As Harish pointed out that one class can't do all the work therefore it needs to call other class to get the job done.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49781
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
I would regards that sort of coupling as “good”. It is a case of HAS-A; an Aeroplane HAS-AN Engine, HAS-[TWO] Wings, etc. Composition.
 
Harsha Smith
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can achieve loose coupling through "Dependency Injection".
 
Hebert Coelho
Ranch Hand
Posts: 754
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is why I like the Demeter Law. It reduces the coupling.

I wrote a post about it, if you are interested, here is the link: Demeter Law
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic