Possible Duplicate:
why we need interface in java?
I refered the following link
can anyone explain any reason why we are using interface in java?
Other than
- multiple inheritance is possible with interfaces.
- polymorphism
Possible Duplicate:
why we need interface in java?
I refered the following link
can anyone explain any reason why we are using interface in java?
Other than
Other than what you have mentioned, interfaces are a good way of exposing a set of functions (API) without divulging any information on their implementation.
By definition, when a class implements
an Interface
, it is agreeing to implementing a series of methods. This will allow any caller to use these methods without:
Adding to all other posts , Interfaces are more abstract
where Interfaces
form a contract between the class
and the outside world, and this contract is enforced at build time by thecompiler
. If your class
claims to implement an interface
, all methods
defined by that interface
must appear in its source code before the class
will successfully compile
.
Interfaces separate the functionality an object offers from its implementation.
You can provide a reference to an interface without needing to know which implementation is actually used.
Interfaces also make testing easier as you can create dummy implementations to test your code against. Libraries like EasyMock and JMock make this easier to do.
When a class implements/realizes multiple interfaces it is not multiple inheritance. The inheritance/extension relationship in fact only holds between constructs of the same kind (an interface may extend another interface or a class another class). The relationship between a class and an interface is realization or implementaiton, not extension. Such differentiation do avoid the multiple inheritance (in Java a class can extend at most one class) and consequently many problems (i.e. diamond problem http://en.wikipedia.org/wiki/Diamond_problem, etc.) eventually improving the design quality
One thing that wasn´t mentioned yet: you can design your program with several design patterns with interfaces (define the APIs). This will later on help you a lot with big software implementations. Maybe you want to read something about it: http://en.wikipedia.org/wiki/Software_design_pattern