• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Interface benefit

 
Ranch Hand
Posts: 63
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Though I understand the mechanics of using an interface, I'm not fully appreciating its usefulness. I realize they're very important, but why? After all, I'm the one who has to provide all the code to perform the function I require, so how is it benefiting me?

Thanks,
Dan
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Dan Bromberg:
Though I understand the mechanics of using an interface, I'm not fully appreciating its usefulness. I realize they're very important, but why? After all, I'm the one who has to provide all the code to perform the function I require, so how is it benefiting me?

Thanks,
Dan



Even though you have to create the code for the interface implementation methods to work, the code calling your object doesn't have to know how it works, doesn't have to know even what class it is working on, just that the object implements that interface, and that it will work as expected. This is tremendously liberating. Have a look at an article on one of the software design patterns such as the observer pattern (you use this for instance anytime you code using Swing) and you'll see the power and beauty if this idea.
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd look at it from a different angle. Interfaces come in handy when you are working with other poeple, teams, or even companies. I write an interface. I write my code to take objects that implement that interface. I then go to the person/team/vendor and say "make your object implement this interface".

When they do, they can then send me objects that i've never dreamed existed, and my code works.

a year from now, when we decide to go with a new vendor, I can sit on the beach drinking Mai-tais after handing them my interface. THEY implement it, and I get a nice tan. When they're done, I know my code already works, so life is GOOD!!!
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
interface force anybody who implement it to overide the method. And also there is common also to define static final Object in the interface. It make the program to be as modular as possible by use Interface.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are a number of situations in software engineering when IT IS IMPORTANT FOR DISPARATE GROUPS OF PROGRAMMERS TO AGREE TO A "CONTRACT" that spells out how their software interacts. Each group should be able to write their code without any knowledge of how the other group's code is written. Generally speaking, INTERFACES ARE SUCH CONTRACTS.

for clear understanding of interfaces go to sun website:
http://java.sun.com/docs/books/tutorial/java/IandI/createinterface.html
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See This Other Thread with similar questions.
 
reply
    Bookmark Topic Watch Topic
  • New Topic