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

Interface

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Ranchers,,
Can anyone please explain me that why to have interfaces in java.
What purpose does it provide?
In interfaces the methods have no implementation.Then of what use it is provide? Instead of implementing
the interface methods we could write the methods directly into our class.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might start by reading the Sun Java Tutorial section on Interfaces and Inheritance.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interfaces and abstract classes and inheritance are several ways Java gives us to use polymorphism, that is different objects that accept the same messages (method call) but do different things.

For example, if I write this:

you can call my method with any implementation of the List interface. You can use LinkedList, ArrayList or some new List that you make up. The compiler will make sure that whatever you pass implements List so I can safely call any method defined by List.

Interfaces are the "most abstract" of the tools we have, which often makes them a good choice when you want to write polymorphic behavior. There is an FAQ entry about choosing between interface and abstract class.

Does that make sense?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic