• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Some issues related with interfaces

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear friends,
I am a newbee in java and i was working with JDBC, i found a very strange thing or may be i dont hav that much knowledge till now. When we eastablish a connection we use atleast three different objects of the following classes :

1) Connection
2) Statement
3) ResultSet

Now as-per my knowledge all the above three are interfaces and in our programs we call different methods using these three objects. Calling a method using an interface object is only possible when the interface object reffers to the object of a class which implements that interface. But these objects dont have the reffernce to any class which implements them so how are we able to call the methods using these interface objects ??

Please help me keeping this thing in mind that i am a newbee.

Thnaking you all
Simrat Singh
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Simrat,

Most of the JDBC api which is part of JDK is full of interfaces. There
are different implementations of these interfaces done by different
JDBC driver vendors. We need not worry about the actual implementing
classes since each non-abstract implementating class will have
implementation of the methods and we may invoke these methods.
 
Simrat Singh
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are quite right dear but still there are somethings which make me confuse. Some of them are the following

Connection con = DriverManager.getConnection
( "jdbc:myDriver:wombat", "myLogin","myPassword");

Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT a, b, c FROM Table1");
while (rs.next()) {
int x = rs.getInt("a");
String s = rs.getString("b");
float f = rs.getFloat("c");
}
Now in the above code talking about the first line, the DriverManager.getConnection() method returns a Connection object only, so how is it possible to call any method using that connection object.
Now there is one solution in my mind which i think may be the case, what i think is that may be in the getConnection method a Connection object gets created and that connection object is being assinged a refference of any class which implements Connection interface. After that this connection object is returned by the getConnection method.
Now please let me know if i am thinking right and please keep in mind that i m a newbee so please explain thoroughly.

Thanking you
Simrat Singh
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have to distinguish between an interface (like Connection) and a class that implements that interface. There is no "interface object", not does an interface "refer to an object".

What DriverManager.getConnection does is to return an object that implements the Connection interface. If you're curious what kind of object it is, you can use con.getClass().getName() to find out. The same goes for the stmt and rs objects - they implement their respective interfaces, but the actual implementing class will be different depending on which driver you use.

The "con" object may implement many more methods, but you can access only the ones that are part of the Connection interface.
 
Simrat Singh
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Sir
 
reply
    Bookmark Topic Watch Topic
  • New Topic