A Basic Question

Connection con=DriverManager.getConnection(url,user,pwd);

con.createStatement();

Because "Connection" is an interface but method- "DriverManager.getConnection()" can return an instance of Connection.

As I know, an interface can not be instantialized.

Why it is valid to do so.

And "createStatement "is an abstract method of interface Connection.

Why it do not need to be rewritten when connected to a database.

I am a new comer and thank you for your attention.

[525 byte] By [javanewcomer] at [2007-9-26 4:25:00]
# 1

Hi,

Actually, your question is good. the answer is as the following:

As you said, you can't instatiate an object from any interface. But if you have an object of a class that implements that interface, you can referance that object by a referance of that interface without explicit casting.

For example, assume that you have th foloowing:

////////start////////////////////

public interfece Inter{

.......

}

public class Cls implements Inter{

........

}

//////////end///////////////

Now, the folowing statements are correct

////////////////start////////

Cls cls=new Cls();

Inter inter=cls;

///////////end////////////

Also if you have a method that have the return type as Inter, the return value can be an object of class Cls. As the following:

////////start/////////

public Inter meth(....){

........

Cls c=new Cls();

........

return(c);

}

/////////end///////////

Now you can do the following:

///////start/////////

Inter inter=xxx.meth(...);

/////end//////////

Actually that is what in the getConnection method.

I hope that you've got the idea.

Regards

Marwan Dardounh

EngMarwan at 2007-6-29 17:32:36 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...