tags:

views:

125

answers:

4

I came across several interfaces while learning JDBC - Connection, Statement, ResultSet etc...

Does this imply that some classes somewhere, hidden from me, are implementing these interfaces, and providing their references when I need it? Is this because they need to be implemented differently depending on the Driver I'm using?

+2  A: 

Yes, you are right. Implementation for these interfaces can be found in the database JDBC drivers.

Johannes Weiß
A: 

Is this because they need to be implemented differently depending on the Driver I'm using?

For example. It's general best practice to code to an interface instead of an implementation (= class, in this context), for multiple reasons. One of them has been mentioned by you: related but different implementations can share a common interface. Another point is that one particular implementation can be changed behind the scenes without requiring changing the interface as well: let's say the next implementation of the driver needs to use another class. It's enough to implement the existing interface.

Now imagine that instead of interfaces, the code dealt with class types. Now, if the implementation changed, these interfaces would need to be changed as well. This would mean that every user (including you) of the API had to change their code as well!

Konrad Rudolph
A: 

As Johannes says, the actual DB drivers implement these classes. The point being that you can switch drivers without actually making any changes to your code.

oxbow_lakes
+1  A: 

In the specific case of JDBC drivers, you're correct. The individual driver jars hold the implementations of the necessary interfaces.

In other cases, such as Calendar for one well-known example, you can check the documentation for known subinterfaces and direct implementations. Of course Sun's documentation will only list the subinterfaces and subclasses that exist in the core Java libraries. You may find many other implementations in 3rd party libraries such as Apache Commons, or in your own code.

Bill the Lizard