Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

when to use interface

 
ahamed irshad
Ranch Hand
Posts: 31
C++ Notepad Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi to all,

i know the concept of interface.but i have no clear cut idea about when should i use interface.thanks in advance
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15369
40
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are many reasons why you might want to use an interface.

One example is to decouple "how a service looks" from the actual implementation, which allows you to have different implementations (that all "look the same" from the outside). A client program (that uses the service) is programmed against just the interface, so that different implementations can be plugged in at runtime. An example of this are JDBC drivers. In your program that uses JDBC, you just use the interfaces (for example Connection, Statement, and ResultSet). The JDBC driver implements those interfaces with classes that are for a specific database (for example Oracle or MySQL), but the client program doesn't need to know the actual implementation classes.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic