• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using Interface and increse the loose coupling  RSS feed

 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi .

This question is regarding coupling .The below program is of tight coupled .


so always use Interfaces . Can anybody please tell me how using a Interface will result in Loose Coupling .


 
Ulrika Tingle
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ravi Kiran V wrote:
so always use Interfaces . Can anybody please tell me how using a Interface will result in Loose Coupling .


It really is inheritance that decreases coupling, and multiple inheritance even more so. Interfaces are Java's way of handling multiple inheritance so they tend to show up in the "write more flexible code" situation.

In your case you just introduce a more abstract supertype to Car like say Vehicle, like



It really doesn't matter whether Vehicle is a class, an abstract class or an interface. The important step was that your code no longer is hardcoded for Car objects anymore. It will now potentially work with a lot more objects, namely all those objects that may inherit Vehicle (like Car, Bicycle, Boat, Airplane, Spaceship, you name it). Your code has become less coupled and more flexible thanks to abstraction by the way of inheritance.
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you .

But here speed is a property . But what in case if i need to call a Method (Which is possible only in case of a class )and tight coupling is only solution .
 
Ulrika Tingle
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ravi Kiran V wrote:Thank you .

But here speed is a property . But what in case if i need to call a Method (Which is possible only in case of a class )and tight coupling is only solution .


Well, a man's got to do what a man's got to do, as they say.

You cannot remove all coupling between types because then your program would be meaningless. There will always be a certain amount of coupling but still, more often that not you can reduce it. It always involves introducing a common supertype (often an interface), but how you do it may vary depending on the situation and your needs. The trick is to arrive at the right amount of coupling.
 
Rok Štelcer
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Ulrika Tingle wrote:
Ravi Kiran V wrote:Thank you .

But here speed is a property . But what in case if i need to call a Method (Which is possible only in case of a class )and tight coupling is only solution .


Well, a man's got to do what a man's got to do, as they say.

You cannot remove all coupling between types because then your program would be meaningless. There will always be a certain amount of coupling but still, more often that not you can reduce it. It always involves introducing a common supertype (often an interface), but how you do it may vary depending on the situation and your needs. The trick is to arrive at the right amount of coupling.

Perhaps what puzzles Ravi is following line:
myVehicle.speed += speed;
I'd have to say that this example has nothing to do with loosely coupling ... perhaps more with a tight.

Loosely coupled design principle has to do with interfaces and methods and not with accessing member variables directly.


Regards,
Rok
 
Ulrika Tingle
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rok Štelcer wrote:Perhaps what puzzles Ravi is following line:
myVehicle.speed += speed;


Well, I seems I didn't pay enought attention to the example. I was so preoccupied with the issue of coupling. It does look like the OP reallty is asking about how to increase encapsulation.
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well i understand that , Loose coupling means writing a DAO Layer (An interface and the implementation classes) to access the DataBase .

Am i right ??
 
Campbell Ritchie
Marshal
Posts: 55770
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . but loose encapsulation tends to go with tight coupling.
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell i did not get you (I think my brain is filled with Mud )

Can you please explain
 
Campbell Ritchie
Marshal
Posts: 55770
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That was actually intended for Ulrika . . .

If you don't tightly encapsulate the data on your class, then you can allow tight coupling. Allowing non-private fields is one way to breach encapsulation.
 
afsar khan
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suppose i need to access the method of class Car which is more specific to this class only and nt mention in the Vehicle Interface. In that case i have to provide the stronge coupling... M I right???
 
Campbell Ritchie
Marshal
Posts: 55770
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For reasons explained here, don't write nt or M.

And I don't think you are right, no.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!