• Post Reply Bookmark Topic Watch Topic
  • New Topic

methods definition that interfaces have  RSS feed

 
Rod Taylor
Greenhorn
Posts: 25
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lately I've been completely puzzled by the methods available on Java API's definition of interfaces. Let met give you an example of code:

...
Connection con = DriverManager.getConnection(url);
Statement stmt = con.createStatement();
ResultSet result = stmt.executeQuery(sql);
...

Now, when I looked at Java API, it says that Connection, Statement, and ResultSet are interface. So, on the second and third lines of the above code, whose createStatement() and executeQuery() methods are invoked? I thought that an interface only contains declaration of the methods. How can an empty methods do any computing/calculation?

Please shed some light on the problem. Thanks.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rod Taylor wrote:Now, when I looked at Java API, it says that Connection, Statement, and ResultSet are interface. So, on the second and third lines of the above code, whose createStatement() and executeQuery() methods are invoked? I thought that an interface only contains declaration of the methods. How can an empty methods do any computing/calculation?

Please shed some light on the problem. Thanks.

Simple answer to your first question: Dunno - and you're not supposed to.
It's one of the basic tenets of OO programming - Information hiding.

All you know is that it's an object of a class (presumably that the designer of the Driver wants to keep private) that implements one of the interfaces you mentioned. And that's all you need to know.

You'll see something similar in the Collections class when you call, for example unmodifiableList() - although maybe you didn't relaize it.
It returns a List, sure enough, but it can't possibly be the same one that you passed into it because you can't modify it any more.

Winston
 
Rod Taylor
Greenhorn
Posts: 25
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your answer. I think I get the gist of it. So, there is some 'background' mechanism that this interfaces have which makes these methods work. Right? I don't need to know, and all I need to do is make use of it. Hhmm, that's quite cool actually. Just like buying things: I don't need to know how it's made-just use it to my advantage.

Thanks again for your answer!
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to know exactly what class is being used to implement the interface, it's easy to find out. Just print out the value of, for instance, connection.getClass().getName().

But this is only for interest's sake. As Winston says, you should never need to know this. One of the advantages it has is that the providers of that library are free to switch to another class implementing the same interface in future if they want to. Because you're only referring to the interface it won't matter to you.]

And welcome to the Ranch!
 
Rod Taylor
Greenhorn
Posts: 25
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your warm welcome and answer (and the coding tip!)
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rod Taylor wrote:Thanks for your warm welcome and answer (and the coding tip!)

Good that you recognized it as a tip: it's called programming to the interface, and it's a very good thing to learn about.

Winston
 
Rod Taylor
Greenhorn
Posts: 25
Java Netbeans IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow, thanks a lot Winston! It's a really good article you sent me! I LOVE this forum!!
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rod Taylor wrote:Wow, thanks a lot Winston! It's a really good article you sent me! I LOVE this forum!!

We aim to please. You're welcome.

Winston
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!