Interfaces: Serenity now!

Readers pepper Java Q&A with interface questions

October 26, 2001

Q: A reader asks:

How is it possible to call methods that belong to an interface when it is not possible to define one in an interface?

As an example, we use the next() method from the ResultSet while using JDBC (Java Database Connectivity).

How is this achieved?

Another reader asks:

I have a JDBC question: You cannot directly instantiate an interface. Then how can a statement like DriverManager.getConnection("something") return an object of Connection type though Connection is an interface?

And yet another:

I thought interfaces had methods without implementation. How does this work since an interface shouldn’t have a body?

A:
Say you have an object called MyDBConnection that implements Connection:

public class MyDBConnection implements Connection
{
    // bunch of code omitted
}

You can state any of the following:

MyDBConnection conn1 = new MyDBConnection();
Connection conn2 = conn1;
Connection conn3 = new MyDBConnection();

In the case of a method, you can state either of the following:

public MyDBConnection getConnection()
{
    return new MyDBConnection();
}

or

public Connection getConnection()
{
    return new MyDBConnection();
}

As you can see, when an object implements an interface, you can cast the object to the interface type. Keep in mind that when you do so, you lose access to any methods that do not appear in the interface definition.

Why would you want to cast to the interface type? Flexibility, that’s why.

What you lose in functionality (those methods you lose when you cast to the generic interface), you make up in plugability.

By programming to an interface, your program doesn’t get tied to a specific implementation of an interface. Instead, you can use any implementation in your program. Take the DriverManager.getConnection() method for example. By defining the method as returning Connection, the method can return any object that implements the Connection interface, making getConnection() very flexible.

As an alternative, you could define getConnection() so it returns a specific Connection implementation. If you do so, however, you can never return an alternate Connection implementation. Instead, you become tied to that specific implementation of the Connection interface. With this in mind, the JDBC libraries must be generic so they can map to any type of database. That’s why getConnection() returns Connection objects.

Look again at the MyDBConnection example. Assume you want to change databases. To do so, you’ll need a new Connection implementation; let’s call it MyNewDBConnection. If you programmed the getConnection() method to return Connection, you simply need to change getConnection() so that it creates the new implementation:

public Connection getConnection()
{
    //return new MyDBConnection();
    return new MyNewDBConnection();
}

No other changes are needed to use the MyNewDBConnection implementation of Connection.

Now, if you had programmed getConnection() to directly return MyDBConnections, you would need to change the method signature and any other object that calls the method.

For more information see Wm. Paul Roger’s excellent JavaWorld article, “Reveal the Magic Behind Subtype Polymorphism.”

Tony Sintes is an
independent consultant and founder of First Class Consulting,
Inc., a consulting firm that specializes in the bridging of
disparate enterprise systems and training. Outside of First Class
Consulting, Tony is an active freelance writer as well as author of
Sams Teach Yourself Object-Oriented Programming
in 21 Days (Sams, 2001; ISBN: 0672321092).

Source: www.infoworld.com