• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

methods definition that interfaces have

 
Greenhorn
Posts: 27
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 27
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 27
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your warm welcome and answer (and the coding tip!)
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 27
Netbeans IDE Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Hang a left on main. Then read this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic