Results 1 to 8 of 8
  1. #1
    vasanthj is offline Member
    Join Date
    Dec 2010
    Posts
    5
    Rep Power
    0

    Default Interface - Implementation link

    How does an API know which implementation to use? For example, if I am using the JDBC API in my code, how does the JVM know to call the correct implementation of this API. Let's say I have two driver jars that implement this JDBC API in my classpath. Which one will get called? Is this based on the DriverManager?

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,939
    Rep Power
    8

    Default Re: Interface - Implementation link

    I would recommend providing an example that demonstrates exactly what you're talking about. You might want to use the List interface as an example.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Interface - Implementation link

    For JDBC it's the DriverManager.
    Each JDBC implementation has a different connection String.
    MySQL = jdbc:mysql
    Derby = jdbc:derby
    Oracle = jdbc:oracle
    etc etc.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    vasanthj is offline Member
    Join Date
    Dec 2010
    Posts
    5
    Rep Power
    0

    Default Re: Interface - Implementation link

    Thanks for the prompt reply. It makes sense now. Can you tell me how this interface-implementation works for JAX-RS -- Jersey? Sorry if i am asking too much.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Interface - Implementation link

    Aren't the implementations called something different?
    In whatever setup file you define what implementation you are going to use (similar to defining which JDBC driver in your connections string).
    Exactly how generally depends on your framework.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    AeneasTroy is offline Arma virumque cano
    Join Date
    Oct 2012
    Location
    Indianapolis
    Posts
    20
    Rep Power
    0

    Default Re: Interface - Implementation link

    In order to fix this problem, you could ONLY import the required class:
    Java Code:
    import java.sql.Datasomething;
    import com.jdbc.mysql.Driver;
    Another way is to instanciate the class with the full class name:
    Java Code:
    com.jdbc.mysql.Driver myDriver = new com.jdbc.mysql.Driver();
    This way, there are no mixups.

    Sometimes, old versions and new versions of the same class may exist together. I am not sure how netbeans resolves it.
    Last edited by AeneasTroy; 10-05-2012 at 10:06 AM.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Interface - Implementation link

    Quote Originally Posted by AeneasTroy View Post
    What you are thinking of is totally different, and sometimes...
    For example, java.sql.* has a lot of the same classes
    as for example com.jdbc.mysql.*;

    In order to fix this problem, you could ONLY import the required class:
    import java.sql.Datasomething;
    import com.jdbc.mysql.Driver;

    Another way is to instanciate the class with the full class name:

    com.jdbc.mysql.Driver myDriver = new com.jdbc.mysql.Driver();
    This way, there are no mixups.

    Sometimes, old versions and new versions of the same class may exist together. I am not sure how netbeans resolves it.
    That's not how you use either of the examples the OP has given.
    JDBC does not work like that (you never import the actual Driver you are using, that's the job of the DriverManager or DataSource), and neither do the other similar frameworks (like JAX-RS, or any of the XML stuff).
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    AeneasTroy is offline Arma virumque cano
    Join Date
    Oct 2012
    Location
    Indianapolis
    Posts
    20
    Rep Power
    0

    Default Re: Interface - Implementation link

    I know, I should have left this question alone.
    Please disregard my response, I totally didn't understand the question.

Similar Threads

  1. interface implementation issue
    By sat123stud in forum New To Java
    Replies: 1
    Last Post: 08-29-2011, 09:00 AM
  2. ItemStateChanged Interface Implementation
    By collwill in forum New To Java
    Replies: 3
    Last Post: 04-19-2011, 04:36 AM
  3. Interface Implementation
    By Samyx in forum New To Java
    Replies: 3
    Last Post: 12-02-2009, 12:20 PM
  4. Interface and implementation erorr
    By mihael in forum Advanced Java
    Replies: 1
    Last Post: 12-07-2008, 11:06 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •