Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Code To Interfaces Doubt???

 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all for answering, but I'm moving this thread which is not related to SCWCD.
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot guys. You all have explained the concept so nicely.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic