72

In Java to implement multiple inheritance we use interfaces. Is it the only use of interfaces? If yes, what is the main use of interface in Java? Why do we need interfaces in Java?

Nathan2055
  • 2,283
  • 9
  • 30
  • 49
Vinoth Kumar
  • 3,243
  • 7
  • 24
  • 13
  • 4
    possible duplicate of [Why we are Implementing Interfaces ?](http://stackoverflow.com/questions/1583654/why-we-are-implementing-interfaces) – Thilo Aug 20 '10 at 06:00
  • 1
    Also a good explanation [here](http://programmers.stackexchange.com/a/131334/146150). – Bruce Sun Jul 05 '16 at 03:15

6 Answers6

55

I would say the main use is polymorphism, or the ability to perform the same operation on a number of different objects. If different objects all implement the same interface and have the same method, you can store all of those objects in a Vector, for example, and iterate through the Vector calling that method on each one.

  • 2
    Rob I agree with you but I would add that by separating the concept of interface realization from class extension (which actually clash in c++) you can have polymorphism without multiple inheritance – Andrea Sindico Aug 03 '12 at 10:26
49

I was also thinking about how interfaces are used. I hope this will help others:

An interface is a contract (or a protocol, or a common understanding) of what the classes can do. When a class implements a certain interface, it promises to provide implementation to all the abstract methods declared in the interface. Interface defines a set of common behaviors. The classes implement the interface agree to these behaviors and provide their own implementation to the behaviors. This allows you to program at the interface, instead of the actual implementation. One of the main usage of interface is provide a communication contract between two objects. If you know a class implements an interface, then you know that class contains concrete implementations of the methods declared in that interface, and you are guaranteed to be able to invoke these methods safely. In other words, two objects can communicate based on the contract defined in the interface, instead of their specific implementation.

Secondly, Java does not support multiple inheritance (whereas C++ does). Multiple inheritance permits you to derive a subclass from more than one direct superclass. This poses a problem if two direct superclasses have conflicting implementations. (Which one to follow in the subclass?). However, multiple inheritance does have its place. Java does this by permitting you to "implements" more than one interfaces (but you can only "extends" from a single superclass). Since interfaces contain only abstract methods without actual implementation, no conflict can arise among the multiple interfaces. (Interface can hold constants but is not recommended. If a subclass implements two interfaces with conflicting constants, the compiler will flag out a compilation error.)

from: http://www.ntu.edu.sg/home/ehchua/programming/java/J3b_OOPInheritancePolymorphism.html#zz-6.6

Michal
  • 31
  • 6
Thein
  • 3,940
  • 2
  • 30
  • 34
19

In addition to these responses I would say the most important use for interfaces is to reduce coupling between components in your software.

An interface allows to represent an agreement between classes on how they will talk to each other without being tied to the actual implementations.

This allows us to replace implementations by others (very useful for testing, or changing use cases) without changing the compiled code.

hakusaro
  • 133
  • 2
  • 10
Peter Tillemans
  • 34,983
  • 11
  • 83
  • 114
6

You need them so you can type your objects outside the hierarchy.

For example, the objects that can be compared can be pretty much anywhere on the object hierarchy - they do not need to have a common ancestor which can be compared. Strings can be compared, Integers can be compared, you could even make your own Frames that could be compared (say, a frame is "less" than another frame if it is more in the foreground - i.e. if it would overlay the other frame). Thus, if you want to refer to a thing that can be compared, you would be forced to declare a variable with the most general ancestor - in this case, Object. This is too general, because then it can also receive values which are not comparable (and would throw errors when you try to compare them).

Thus, the interface Comparable: it selects all the classes that implement the comparison functionality across the subclass-superclass hierarchy.

Amadan
  • 191,408
  • 23
  • 240
  • 301
1

We need interfaces :

  1. To achieve total abstraction.
  2. To achieve security.
  3. Java doesn't allow multiple inheritance but it can be achieved by implementing multiples interfaces.
-6

Some code won't compile without it.

For example, in:

for (String name : list)
{
    System.out.print("\nIn foreach loop: name: " + name);
}

list must implement the java.lang.Iterable interface.

Mihai Iorga
  • 39,330
  • 16
  • 106
  • 107
  • 6
    This is essentially the same as saying you need to use something because we have to. This does not **adequately** answer the question. – rayryeng Jul 06 '14 at 06:00
  • 1
    I know that. I'm simply pointing out that we have to use them in this case, whether we like it or not. I prefer to think of it as analogous to assembling a P.C.: Without some conformity between the hardware connectors, replacing a part such as a power supply or hard disc would be a lot harder if they weren't interchangeable. If this doesn't not adequately answer the question, I like to think at least it helps people understand why they are of use. – Danny Crossley Jul 07 '14 at 08:37