0

In JDBC getConnection is a static method which returns object of Connection Interface? The getConnection method is defined in DriverManager class as a blank body as follows:

@CallerSensitive
public static Connection getConnection(String url, String user, String password) throws SQLException {
    // compiled code
}

I am not getting how it is working? What happens when we return an object to interface? How Connection interface using this object?

How it is using this object for statment in the below line:

Statement s= c.createStatement();

Again how things are working internally in above line?

I want a deeper explanation basically how interface and classes work together to things like this. I know what an interface is, how it is implemented by the classes. It is used to support multiple inheritance and to provide security. But how are these concepts related in this example?

keshav
  • 9
  • 2
  • "_What happens when we return an object to interface_" You get an object that is guaranteed to implement all the methods of the interface - that's the whole point. Now it doesn't matter what actual object is returned, you always know what methods you can call. – takendarkk Dec 29 '17 at 21:11
  • https://stackoverflow.com/questions/383947/what-does-it-mean-to-program-to-an-interface I feel like dupehammering this if you can't explain yourself better than that. – Kayaman Dec 29 '17 at 21:15
  • The point of programming to an interface is that you don't need to care about "how things are working internally". And that makes life a lot easier. If you want to know how things are implemented, you can look at the sources of the implementing classes (JDBC driver in this case). – Mick Mnemonic Dec 29 '17 at 21:23
  • HI,Kayaman I referred your link-"What does it mean to “program to an interface”? ".It didn't cleared my doubt totally however it helps me to understand more on interface but still I want to ask in terms of JDBC.I came to know that Connection,Statement and ResultSet interface is implemented by releted classes in sql package by JDBC provider and getConnection() method returning object of Connection Interface in turn createStatement() returns object of Statement Interface in turn executeQuery() returns object of ResultSet() interface and finally we can use the ResultSet() methods to data I/O. – keshav Dec 30 '17 at 09:34
  • My question is how these methods returning objects without return statement and method body(executeQuery(),createStatement(),next() etc..). I am sorry If my question are too silly I am new in java and want to keep no doubts in my mind. – keshav Dec 30 '17 at 09:37
  • @keshav Why do you think these implementations have no return statement or body? What is the URL where you saw that? – Progman Dec 30 '17 at 11:00

0 Answers0