This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes JDBC and Relational Databases 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 Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "will a function return a refrence of an Interface?" Watch "will a function return a refrence of an Interface?" New topic

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

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
I agree. Here's the link:
subject: will a function return a refrence of an Interface?
It's not a secret anymore!