top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is the use of interfaces in Java?

+1 vote
211 views
What is the use of interfaces in Java?
posted Mar 16, 2016 by Renita

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

The main points to consider [two already mentioned here] are:

Interfaces are more flexible, because a class can implement multiple interfaces. Since Java does not have multiple inheritance, using abstract classes prevents your users from using any other class hierarchy. In general, prefer interfaces when there are no default implementations or state. Java collections offer good examples of this (Map, Set, etc.).

Abstract classes have the advantage of allowing better forward compatibility. Once clients use an interface, you cannot change it; if they use an abstract class, you can still add behavior without breaking existing code. If compatibility is a concern, consider using abstract classes.

Even if you do have default implementations or internal state, consider offering an interface and an abstract implementation of it. This will assist clients, but still allow them greater freedom if desired

answer Mar 16, 2016 by Karthick.c
...