Top 10 in 2008
5 popular archives:
All selections are based on page views.
Best of 2008: A developer's list
JW blogger Dustin Marx names his top 10 technology events of 2008. Highlights include updates to Java SE 6, runtime support in OpenLaszlo 4.2, and the clash of the titans that occurred early in the year, when Sun acquired MySQL on the same day that Oracle announced its acquisition of BEA. No two lists are alike and it's not too late: What were your top 10 for 2008?
Also see:
October 26, 2001
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 theResultSetwhile 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 likeDriverManager.getConnection("something")return an object ofConnectiontype thoughConnectionis an interface?
And yet another:
I thought interfaces had methods without implementation. How does this work since an interface shouldn't have a body?
As you can see, interfaces can be quite confusing. However, the explanation is simple. An interface defines a type. When
an object implements an interface, you can cast the object back to its interface type and treat it as if it were an instance
of the interface.
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."