aspose file tools
The moose likes Beginning Java and the fly likes interface problem - !@#$%^&* Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide 1Z0-808 this week in the OCAJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "interface problem - !@#$%^&*" Watch "interface problem - !@#$%^&*" New topic

interface problem - !@#$%^&*

raajendra nath

Joined: Jul 18, 2006
Posts: 4
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 ....................");

( 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

Joined: Dec 04, 2005
Posts: 81
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

Joined: Jul 18, 2006
Posts: 4
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

Joined: Jul 08, 2003
Posts: 24189


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


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.

[Jess in Action][AskingGoodQuestions]
raajendra nath

Joined: Jul 18, 2006
Posts: 4
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

Joined: Jul 11, 2001
Posts: 14112
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.

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
raajendra nath

Joined: Jul 18, 2006
Posts: 4
thanq very its clear for me.......
we are doing this from past three gawd!!!i got answer in just 18 mins.......once again thanx to every one
I agree. Here's the link:
subject: interface problem - !@#$%^&*