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 ?
Asked
Active
Viewed 51 times
-3
-
1Java? Add the language tag. – Yu Hao Nov 01 '14 at 15:25
-
possible duplicate of [When should one use interfaces?](http://stackoverflow.com/questions/1686174/when-should-one-use-interfaces) – Sam Hanley Nov 01 '14 at 15:44
1 Answers
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