views:

92

answers:

5
Connection con = DriverManager.getConnection("jdbc:odbc:MyDataSource1");

This is a JDBC programing line. In this line, we are creating an object for Connection interface. How it is possible?

+7  A: 

The interface is the reference type for your "con" variable. The implementation for the interface comes from the JDBC driver that you registered for your problem. The DriverManager returns the driver implementation for Connection and all the other interface types in java.sql.

duffymo
+3  A: 

The object that the righthand side of the assignment produces is a subtype of Connection: an instance of a Class that implements the Connection interface. You don't need to know what the exact type is, as a result of which you can use the same line with different connection Strings and receive a MySqlConnection, DB2Connection, OracleConnection or WhatHaveYouConnection instance, which properly handles the subsequent database calls that you make through the con variable.

See, for instance:

http://stackoverflow.com/questions/383947/what-does-it-mean-to-program-to-an-interface

http://stackoverflow.com/questions/1848442/what-exactly-is-interface-based-programming

http://stackoverflow.com/questions/2697783/what-does-program-to-interfaces-not-implementations-mean

Confusion
+1  A: 

DriverManager.getConnection() is a static factory method that returns an instance of a class that implements the Connection interface. This approach permits programming to an interface for reduced dependency, also discussed here.

trashgod
+2  A: 

DriverManager.getConnection("jdbc:odbc:MyDataSource1"); somehow creates an object which implement the Connection interface.

As long as the object returned is of the Connection type, you can hold a reference to it through a variable with Connection type. You don't need to care what the actual object is as long as it's a Connection - that is, the object returned implements the Connection interface.

It's the same principle as e.g.

public interface Instrument {
  public void play();
}

public class Vuvuzela implements Instrument {

  public void play() {
     System.out.println("Bzzzzzzzzzzzzzz");
}

...
public static Instrument getInstrument(String name) {
 if("Vuvuzela".equals(name)) {
    return new Vuvuzela();
 } 
 return null;
}

...
//get an instrument, we don't care what the actual object
//is as long as it is an Instrument.
//in this case the instrument variable will "point" to n
//Vuvuzela object but we don't need to care about that
Instrument instrument = getInstrument("Vuvuzela");
intrument.play();
nos
That should probably be:while (true) { System.out.println("Bzzzzzzzzzzzzzz"); }
Mike Baranczak
getInstrument() should return a NOPInstrument and not null. Otherwise you will debug NullPointerExceptions at 3 in the morning some day.
Thorbjørn Ravn Andersen
+1  A: 

You need to understand that an Java interface is a contract: The object implementing the interface promises to have all the things presented in the interface!

What else the object is capable of, is irrelevant to this particular interface and it is actually frequently the case that there are multiple possible candidates for a given interface and the calling code basically doesn't care (or need to care) which one it receives.

In the JDBC-case, you are asking for a database connection, and the DriverManager picks one according to the string you pass and returns it to you. All you need to know is that the one you get has all the methods listed in the Connection interface contract, which you can then use as you need.

So, when an interface used on the LEFT side on an assignment it just means that you can get any object as long as it implements the interface. When a class is used on the left side, you must present an object of that class (or a subclass) which has proven to be much more restrictive.

Thorbjørn Ravn Andersen