Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

why interfaces

  Asked By: Steven    Date: Nov 19    Category: Java    Views: 649

i got a doubt on interfaces.

why shd we use interfaces(plz do not say simply for multiple inheritance)
why shd all variables in an interface are final..
(plz do not say simply all variables in interfaces are final implicitly )

because ..i need to a clear clarification..
i serched a lot for this but could not find ..exact reason..



2 Answers Found

Answer #1    Answered By: Leonard Pierce     Answered On: Nov 19

if you study design patterns, one of the mottos is to design to
the interface. So, the reason as to why is all over the place. If you
go to Javaworld, you can find  an abundance of articles on why to
program to the interface.

An important reason to have interfaces  is to support multiple  type
inheiritance. Since classes are locked down to single implementation
inheiritance, i.e extending an abstract class, an interface  allows a
developer to make a contract so that an object can inheirit many types
via implements. Now, I can cast my object to the interface and work
with any object that implements the interface in a standard manner. In
this case, a sub perk of interfaces is revealed. It allows for objects
to be substituted by other objects that implement the same interface.
So, if I was to use an API that has been standardized by the JCP, then
I can utilize any vendors implementation of the interface without
having to worry about how the vendor implemented the API.

In addition, a spinoff is that it helps with multi-developer projects.
Some people like to develop apps by breaking it up into chunks. Well,
one of the tasks that comes before deployment is integration. If the
developers decide on an interface/API for the chunks, then the
developers can work seperately without too much impact to integration.

Answer #2    Answered By: Elisabeth Bell     Answered On: Nov 19

interface is a sort of contract. you put all the
mthods in the contract without any implementation. But
when you sign the contract that is implement it you
should proveide the implementation ie bodies to the
methods. interface  is a way of forcing one to
implement a certain design.

Didn't find what you were looking for? Find more on why interfaces Or get search suggestion and latest updates.