• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

code to interface type

 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi...
Anybody explain to me what is benefit By coding to an interface?
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it helps promote reusability.

komal
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if you code to an interface, then the underlying object can be changed to a different object type, as long as that new type still implements the interface.

Example: we each purchase a class called 'searchTool' from a 3rd party that does some searching. it implements the 'searchList' interface.

You write your code to specifically use the 'searchTool'. You call it's methods directly, access specific parts, and use some of it's pieces in your code.

I write my code to use the 'searchList' interface. I ONLY refer to it by the interface name, only use the interface methods.

a year later, they come up with a 'fastSearchTool' class that implements the 'searchList' interface, and it runs 100 times faster. I don't have to change a single line of my code, and I can drop in the 'fastSearchTool' to gain the benefit.

You, however, have to re-write all your code to now use the new class, if you want to gain the benefits. So while I'm sitting on the beach drinking Maitai cocktails, you are in the office, slogging through your code to make your changes.
 
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

You, however, have to re-write all your code to now use the new class, if you want to gain the benefits. So while I'm sitting on the beach drinking Maitai cocktails, you are in the office, slogging through your code to make your changes





If only the real world worked like that!
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A coder can dream, can't he?
reply
    Bookmark Topic Watch Topic
  • New Topic