| 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
|
 |
 |
|
|
subject: will a function return a refrence of an Interface?
|
|
|