File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

code to interface type

 
Raj chiru
Ranch Hand
Posts: 142
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi...
Anybody explain to me what is benefit By coding to an interface?
 
Komal Amaresh
Ranch Hand
Posts: 67
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it helps promote reusability.

komal
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12022
25
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Brian Legg
Ranch Hand
Posts: 488
  • 0
  • Mark post as helpful
  • send pies
  • 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
Bartender
Pie
Posts: 12022
25
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A coder can dream, can't he?
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic