• 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

Why java Interface?

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

We can feel the advantages of class inheritance from at least sharing and reusing the super class's methods. But in an interface, there are only abstract methods, and we have implement all the methods in the class that implements this interface, why do we still use interface? Why don't we define the methods and write their code directly in the class without implementing any interface or something like that? In other words, what are the advantages of using interface? I have read
some books, in which the authors just say it's a protocol and so on. I still can not understand the necessity of using interface. Can anybody give me a sample to illustrate this?

Thanks
Srinivas
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OO programming talks about behaviour, and Interfaces define behaviour without specifying how that behaviour is implemented.

In a simple sense, consider the java.lang.Thread Class and the java.lang.Runnable Interface.
Java does not allow multiple inheritance, some once you have a base concrete class you're stuck with it. Therefore any class that wanted to run would be forced to extend Thread and also it would not be possible to make other classes that do no already extend Thread to be able to run.

The Collections classes make heavy use of Interfaces so that you can swap one Collections implementation for another easily. If you force users to Pass ArrayLists to your methods than that works fine until someone wants to use a Vector, LinkedList, stack or even a class of their own.

The JDBC API makes even heavier use of interfaces by defining database operations in terms of interactions between interfaces without very few concrete classes. This allows Driver writers to hide the concrete implementation under the interfaces and as long as they behave in the manner defined you never need to know the difference.

This just scratches the edge, but by defining code in terms of behaviour and not concrete implementations you provide looser coupling between the parts and open yourself up to greater flexibility in the future.
 
Srinivas Katta
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot for the Detailed explanation
 
reply
    Bookmark Topic Watch Topic
  • New Topic