• Post Reply Bookmark Topic Watch Topic
  • New Topic

Interface  RSS feed

 
komal ekbote
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • 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.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might start by reading the Sun Java Tutorial section on Interfaces and Inheritance.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • 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?
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!