• 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 ...
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Al Hobbs
  • Piet Souris
  • Himai Minh

why use interfaces at all?

Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if interfaces are mere something that a class implements by giving definitions then why use interfaces at all ?
Why not just have a class with a method that defines a method and then we use it .
One of the explanations i got in a book was

Java is strongly typed.When making a method call the compiler needs to be able to check that the method actually exists .
then it gave an example:-
//rearrange a[i] and a[j]

The compiler must know that a[i] actually has a compareTo() method .

Now asuuming class did not implement comparable interface but defined a method compareTo() then still the compiler would have been able to know that a[i] has a compareTo() method or not , so again why use interfaces at all ?
Posts: 76440
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, it is not at all easy for the compiler to check that an object passed to methods like Collections#sort(List). It is much easier to say the parameter type is List<Comparable>, which means the members of the List have to be Comparable (the generics is actually more complicated than that). So the elements would have to extend the Comparable class. But you can't extend two classes. If Comparable is an interface, you can implement several interfaces; look at the String class for example.
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For me the real power of the interfaces is in decoupling components, and that becomes more important in large applications. Interfaces define behavior, while classes define how this behavior is achieved. E.g. you might have an interface

and several implementations of this interface which load properties from different sources like XML, DB, file etc. Then in the main (or any other class) you may create the loader and use interface reference to point to it:

After creating you can pass the loader around to any number of methods/classes, and if in some time you decide to load your properties from a DB, all you will need to do is to change one line:
without touching the rest of the code.

Hope this helps.
when your children are suffering from your punishment, tell your them it will help them write good poetry when they are older. Like this tiny ad:
Garden Master Course kickstarter
    Bookmark Topic Watch Topic
  • New Topic