2

Possible Duplicate:
why we need interface in java?

I refered the following link

prevoius post

can anyone explain any reason why we are using interface in java?

Other than

  • multiple inheritance is possible with interfaces.
  • polymorphism
Community
  • 1
  • 1
Amith
  • 1,907
  • 5
  • 29
  • 48

5 Answers5

6

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:

  • The caller having to worry about how is the function implemented;
  • Whoever wrote the function to worry about exposing internal logic.
npinti
  • 51,780
  • 5
  • 72
  • 96
4

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.

amicngh
  • 7,831
  • 3
  • 35
  • 54
3

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.

Radu Murzea
  • 10,724
  • 10
  • 47
  • 69
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • 1
    and you can compile code against the interface, so the implementation does not even have to exist yet. – Thilo Aug 03 '12 at 10:11
  • If you use are using a mocking library or a `Proxy`, it doesn't have to exist at all. ;) – Peter Lawrey Aug 03 '12 at 10:13
  • Just like USB is an interface and no one needs to care what the device actually does as long as it's implementing the USB interface. It could just use USB for charging, or for transferring data. You as a consumer don't care what the devices actually do, as long as they implement the USB interface and thus don't break your computer. – Prinzhorn Aug 03 '12 at 10:15
  • I think you have a typo: with should be without. – yshavit Aug 03 '12 at 10:16
  • I think the most important thing (with respect to C++) is that by separating the concepts of class extension and interface realization multiple inheritance is avoided by definition and in the end the software quality (see my answer) – Andrea Sindico Aug 03 '12 at 10:21
1

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

Andrea Sindico
  • 7,358
  • 6
  • 47
  • 84
1

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

MisterCal
  • 622
  • 2
  • 7
  • 22
Christian Lendel
  • 410
  • 1
  • 4
  • 13