-3

I know interface is a type which have abstract method and constant variable . the class which implements interface needs to define the body of abstract method , but what is the actual use of interface , I have seen lot of code in which interface have only one abstract method and only one class implements that ,if this is the situation the what is the use of interface ? In case of serializable interface I agree its good. Can any one have better answer in this ?

sharma.mahesh369
  • 985
  • 11
  • 28

1 Answers1

1

Interfaces are for separating methods from their implementations. Clients can deal with interface references and not be concerned about the implementation details.

I'd say your citation of Serializable as good shows you don't know anything about interfaces. The JDK is rife with better examples. Two of the best are the java.util.collections and java.sql packages. Clients can use Map references; developers can choose from HashMap, LinkedHashMap, TreeMap, ConcurrentHashMap, and others. You'd have to rewrite your code to change database vendors if JDBC wasn't based on interfaces.

duffymo
  • 305,152
  • 44
  • 369
  • 561