-2
package jdbc;

import java.sql.*;

public class Mango {

    public static void main(String[] args) throws Exception {
        Class.forName("oracle.jdbc.driver.OracleDriver");   
        Connection con = DriverManager.getConnection("jdbc:oracle:thin:@66.66.66.128:1521:xe","SYSTEM","matrix");
        Statement comm = con.createStatement();
        comm.executeQuery("insert into a values('a',1)");
        ResultSet res = comm.executeQuery("SELECT * FROM A");
        comm.executeQuery("insert into a values('a',1)");
        while(res.next()) {
            System.out.println(res.getString(1) + " " + res.getInt(2));
        }
        System.out.println("con class is "+ con.getClass());
        System.out.println("comm class is "+ comm.getClass());
        System.out.println("res class is "+ res.getClass());
    }

}

output:

con class is class oracle.jdbc.driver.T4CConnection
comm class is class oracle.jdbc.driver.T4CStatement
res class is class oracle.jdbc.driver.OracleResultSetImpl

T4CConnection implements the interface Connection T4CStatement implements the interface Statement OracleResultSetImpl implements the interface ResultSet

If connection, statement and resultset are interface's how am I able to invoke them ? I read here that I can never instantiate an interface, but look at this following program here ...

Community
  • 1
  • 1
Cyclops
  • 45
  • 1
  • 1
  • 4
  • Can you also post the output you get when you run your code? – Code-Apprentice Jun 16 '13 at 19:26
  • _"How can we instantiate the interface directly like it is done below ?"_ What code are you referring to? – Matt Ball Jun 16 '13 at 19:26
  • Look at your code closely, you are instantiating the classes that are implementing the interfaces, not the interfaces themselves! – Curious Jun 16 '13 at 19:28
  • 1
    possible duplicate of [Who implements Connection, ResultSet and Statement interfaces in this java program?](http://stackoverflow.com/questions/17136435/who-implements-connection-resultset-and-statement-interfaces-in-this-java-progr) – amalloy Jun 16 '13 at 19:35
  • Note that the output of your program does *not* indicate that the interfaces are instantiated. The output shows concrete classes which implement the interfaces. – Code-Apprentice Jun 16 '13 at 20:12

2 Answers2

2

The interfaces themselves are not instantiated. Rather the classes which implement these interfaces are.

Example code:

import java.util.ArrayList;
import java.util.List;


public class Interfaces {

/**
 * @param args
 */
public static void main(String[] args) {
    List<String> list = new ArrayList<String>();
    System.out.println("list class is " + list.getClass());
}

}

Output:

list class is class java.util.ArrayList

Note that the class for list is ArrayList, not List. This shows that the interface is not instantiated as already claimed.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
2

The objects that are returned from methods getConnection(), createStatement(), etc are instances of the interfaces. Meaning that implement the methods in the interface and would return true when using the appropriate type and instanceof.

All interfaces are 100% abstract meaning these is no code in the methods, just method signatures. Either your class or its super class must implement all methods in the interface. This is Java's solution to multiple inheritance so as to not cause confusion on which method implementation is invoked. It allows for an object to act like multiple types at the same time, but only allows 1 direct super class.

cogsmos
  • 806
  • 6
  • 11
  • Also if you like mnenomics you can use 'is-a' for interfaces and 'has-a' for properties. For example `oracle.jdbc.driver.T4CConnection` is-a connection while `DriverManager` has a connection. – cogsmos Jun 16 '13 at 19:35