GeeCON Prague 2014*
The moose likes Java in General and the fly likes Coupling question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Coupling question" Watch "Coupling question" New topic
Author

Coupling question

Adam Jordan
Greenhorn

Joined: Apr 26, 2011
Posts: 1
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

Joined: Jul 18, 2011
Posts: 287
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

Joined: Oct 19, 2011
Posts: 54
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

Joined: Oct 13, 2005
Posts: 39087
    
  23
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

Joined: Jul 18, 2011
Posts: 287
you can achieve loose coupling through "Dependency Injection".
Hebert Coelho
Ranch Hand

Joined: Jul 14, 2010
Posts: 754

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


[uaiHebert.com] [Full WebApplication JSF EJB JPA JAAS with source code to download] One Table Per SubClass [Web/JSF]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Coupling question