This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes JDBC and the fly likes will a function return a refrence of an Interface? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "will a function return a refrence of an Interface?" Watch "will a function return a refrence of an Interface?" New topic
Author

will a function return a refrence of an Interface?

Arun Maalik
Ranch Hand

Joined: Oct 25, 2005
Posts: 216
Helo rancher i would like to know one thing that

How a function return the refrence of interface as like see

the getConection() method of DriverManager class return a refrence of Connection interface how it is returning.

please give me a bit of code so that i can see that a method can return the refrence of an interface.

with regard

Arun kumar maalik
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

You can't instantiate an Interface. You can however instantiate a class that implements that Interface. You ask about java.sql.Connection, well Connecitons are provided by your JDBC driver, which is implemented by a database vendor. So when you call this method (remember, Java has methods, not functions):

What is returned is an instance of a vendor specific Conenction implementation.

Here is a simple example of the type of thing that is happening in the DriverManager:

OK, here the interface is a java.util.List, but the method returns an java.util.ArrayList() that implements List. In the getConnection(String) method of DriverManager there will be some logic to ask the driver for an instance of its class that implements java.util.Connection. What could be returned might be an OracleConnection, or a JDTSConnection or whatever.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
 
Don't get me started about those stupid light bulbs.
 
subject: will a function return a refrence of an Interface?