Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

interface problem - !@#$%^&*

 
raajendra nath
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
as far as I know interface is there to support mulitiple inheritence like thing....and it'll consists of all abstract methods.....but when i looked at some interfaces in api like Connection,ResultSet they are calling some methods(like normal classes) and the methods are returning some values too..
for example:

ResultSet rs=Stmt.executeQuery("select ....................");
if(rs.next())

(rs.next method is returning boolean, but resultset is interface )..... my doubt is where and how all these methods for interfaces in api are implemented(sorry if i am missing any basic thing)
thanx in advance
 
Stary Kapec
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ResultSet is an interface, but the object returned by executeQuery() is NOT.
You don't have to care about the class of the object as far you know it implements the ResultSet interface. This is an advantage of interfaces.
 
raajendra nath
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
actually i dint get u....can u give me an example(non-api interface) like
creating some interface like exinterface and giving
exinterface ex;(can i give like this )
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

A lot of people ask this question -- it's a good one, and the answer is enlightening.

A method that is declared to return an interface type can return an instance of any class that implements that interface. The class doesn't have to be a public class -- i.e., it doesn't have to be any documented class, but can be private to the implementation. It might even be a class nested inside some public class (an "inner class".)

If you look at the JDBC API -- the java.sql package -- you'll see that almost everything is an interface. You are probably aware of the idea of a JDBC "driver" -- a package of software that lets you talk to a particular database. A "driver" is actually one or more implementations of every one of those interfaces. JDBC is carefully designed so that you can work with all the classes in a driver without knowing the names of any of them -- you only refer to these classes via the interfaces they implement! So when, for example, you call

java.sql.DriverManager.getConnection("jdbc:foo:MyDatabase");

the class DriverManager looks at all the drivers it knows about, finds the one for the "foo" database, and calls connect() on the FooDriver object. FooDriver.connect() returns an instance of FooConnection, a class that implements the Connection interface. DriverManager.getConnection(), which is declared to return Connection, returns this FooConnection object. You store that FooConnection into a variable of type Connection, without ever knowing the name of the FooConnection class -- it's invisible to you, and in fact, is very likely not a public class.

It works the same way for other interfaces in the Java APIs; for example the Iterator objects returned by ArrayList.iterator(), LinkedList.iterator(), etc, are generally instances of private inner classes of those List implementations. All you know is that it implements Iterator.
 
raajendra nath
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanq for ur welcome and reply......i almost got it!!

so here
Connection egcon=DriverManager.getConnection("jdbc dbc:xxx");

we are assigning implementing class's object to interface......
so is it polymorphism like thing???

and one more doubt...why these classes r made invisible???(am i asking too many doubts ??)
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by raajendra nath:
so is it polymorphism like thing???


Yes, exactly. That's what polymorphism is good for!

and one more doubt...why these classes r made invisible???(am i asking too many doubts ??)


They are in fact not part of the standard Java installation. Only the interfaces are part of Java - the implementing classes are provided by the JDBC driver for the database. Every database vendor provides its own implementation, and might even change the implementation details without you noticing.

That way, JDBC can support databases that don't even exist yet. That is, if you'd write your own database, all you needed to do is writing a JDBC driver (that is, implement the JDBC interfaces with your own classes) and everyone can use JDBC to connect to your database.
 
raajendra nath
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanq very much.......now its clear for me.......
we are doing this from past three days........my gawd!!!i got answer in just 18 mins.......once again thanx to every one
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic