问题
There are many predefined interfaces in java like ResultSet, Connection, Statement etc.An Interface can have only abstract methods (unimplemented methods).So why do we use there methods without defining them first.
for example in following code of jdbc
public class JDBCSample {
public static void main( String args[]) {
String connectionURL = "jdbc:postgresql://localhost:5432/movies;
user=java;password=samples";`
try {
Class.forName("org.postgresql.Driver");
Connection con = DriverManager.getConnection (connectionURL);
Statement stmt = con.createStatement();
ResultSet rs = stmd.executeQuery("select moviename, releasedate from movies");
while (rs.next())
{....do something.....}
}catch (SQLException e)
{e.printStackTrace();}
catch (Exception e)
{ e.printStackTrace();}}
here are we calling abstract createStatement() and executeQuery() method of Connection and Statement interface? If yes then how an abstract method(method without body) can perform some task?
回答1:
When you call DriverManager.getConnection, it returns an instance of org.postgresql.jdbc4.Jdbc4Connection, which is a class that implements the java.sql.Connection interface.
Jdbc4Connection has a method body for createStatement that returns an instance of org.postgresql.jdbc4.Jdbc4Statement, which is a class that implements the java.sql.Statement interface.
Jdbc4Statement has a method body for executeQuery which returns an instance of org.postgresql.jdbc4.Jdbc4ResultSet, which is a class that implements the java.sql.ResultSet interface.
回答2:
Call:
System.out.println(con.getClass());
and notice that the actual object is not of Connection type. This is called polymorphism. Simply put DriverManager.getConnection() returns something that implements Connection. You are invoking methods on that something. This is an advantage of polymorphism - the JDBC driver can later decide to change something and as long as it implements Connection, your code couldn't care less.
Straightforward example:
List<String> list = new ArrayList<String>();
list.size(); //calling abstract method?!? No!
In the example above we are actually calling ArrayList.size().
回答3:
You're calling methods of classes that are implementing this methods.
DriverManager.getConnection returns you concrete implementation of Connection interface, and you don't need to know what exact class is returned. It's main idea of having interfaces.
回答4:
By the time you're actually calling something, the reference you have is to an instance of a concrete subclass which provides all the bodies. The point is that you (as the client) don't need to care what that implementation is - it's provided for you by DriverManager (etc). All you need to care about is that it's "something which implements the given interface" or "something which extends the abstract class".
A simpler example would be to use the collection types:
List<String> list = new ArrayList<String>();
doSomething(list);
...
public static void doSomething(List<String> list) {
// Act on the list
}
Here, doSomething doesn't need to know that we used an ArrayList<E> - it could operate on any kind of List<String>... but as you can see, as it happens we create an instance of ArrayList<E>, which is a concrete class.
回答5:
The particular implementation of the interface you are using, in this case DriverManager, returns a concrete class that implements the abstract methods of the interface.
As you pointed out, the interface only declares the methods that the concrete implementation must provide. The actual implementation class provides the bodies of the those methods.
The DriverManager class and its package provide the missing pieces, returning concrete classes that have complete method bodies. You interact with those classes on your client code side only through the interface, however. The implementation details of the concrete classes are effectively hidden from view.
来源:https://stackoverflow.com/questions/7811265/how-does-abstract-method-of-predefined-interface-like-connection-statement-etc