Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Code To Interfaces Doubt???  RSS feed

 
satya mahapatra
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

How code to inetrfaces is helping to acheive loose coupling and what is advantage of loose coupling.

Can anyone pls explain this with the help of an example.

Thanks,
Satya
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess this question would be better answered at the SCJP forum.
 
satya mahapatra
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Code To Interface is one of the j2ee design principle thats why I have put the question in this forum.
 
Saurabh Kumar
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

"Code to Interface" means you write your program to an abstract type(a super class or an interface) and not to a concrete implementation.

for example:
when you write:

You are coding to the concrete implementation- ArrayList.

whereas when you write:

you are coding to an abstract type: List interface.

So, in the first piece of code, the object "myList" is highly coupled to a particular concrete implementation(ArrayList) whereas in the second piece of code it is loosely coupled.Moreover, doing so, you can achieve better flexibility. You can use 'myList' object for any concrete implementation of the abstract type- List.

Hope this helps.

Thanks and regards,
Saurabh
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for answering, but I'm moving this thread which is not related to SCWCD.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my opinion both declarations are equally tightly coupled.



Neither line will compile if ArrayList class is not present on the classpath at compile time. Any time you mention a concrete type in your code you are tightly coupled to that implementation. However you don't have to couple clients that use your code to that type. A method that returns an ArrayList reveals implementation details, a method that takes an ArrayList as a parameter forces client code into a particular implementation. Not so if List or Collection is used as the parameter or return type of the method.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right, those examples don't quite get to the value of interfaces or abstraction. Let's look at a method:

Because this uses List I can pass it any kind of List ... Arraylist, LinkedList or other List implementations nobody has even thought of yet. If the method had been written:

you couldn't call it with those other things.

That just barely scratches the surface of the goodness of abstractions and interfaces. Does it seem worth while so far?
 
Saurabh Kumar
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats true...
I had the same thing in mind but perhaps couldnot explain in better way as you guys did.

Thanks for your explanations.

Thanks and regards,
Saurabh
 
satya mahapatra
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot guys. You all have explained the concept so nicely.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!