Why do we need interfaces in Java?

JavaInterface

Java Problem Overview


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?

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

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

Solution 3 - Java

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.

Solution 4 - Java

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.

Solution 5 - Java

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.

Solution 6 - Java

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVinoth KumarView Question on Stackoverflow
Solution 1 - Javauser377136View Answer on Stackoverflow
Solution 2 - JavaTheinView Answer on Stackoverflow
Solution 3 - JavaPeter TillemansView Answer on Stackoverflow
Solution 4 - JavaAmadanView Answer on Stackoverflow
Solution 5 - JavaRohini PatilView Answer on Stackoverflow
Solution 6 - JavaDanny CrossleyView Answer on Stackoverflow