java - J2SE interfaces specifications and implementations of those interfaces -


i looking @ rt.jar reasons , there saw packages java.sql.* among others.

in typical jdbc program write (for connection class, example):

import java.sql.connection; 

as per docs, java.sql.connection interface, not concrete implementation, , java.sql.connection in rt.jar.

when write jdbc program, need jdbc drivers, , read jdbc drivers implement interfaces (e.g. java.sql.connection).

so when write in typical java program: (and load jdbc drivers)

import java.sql.connection; 

--> java.sql.connection come rt.jar or driver classes.

from guess, in case java.sql.connection has come rt.jar (as interface), , actual implementation comes driver classes.

if assumption correct, in general need include jar's have interface definitions in order include import.

for example, consider situation:

package com.vipin.myinterface;  public interface interface1 {       public void print(); } 

and if package above interface interface1.jar.

suppose concrete1.java implements interface:

package com.vipin.concrete1;     public class concrete1 implements interface1 {           public void print () {                //code          }    } 

and packaged in jar --> concrete1.jar.

now, suppose writing application uses print() method, need include both these jar's?

the case java.sql.connection driver provides implementation classes , other interfaces java.sql.statement, java.sql.resultset, , on. magic of binding interface proper class implementation happens in method drivermanager#getconnection, calls internal method private static connection getconnection(string url, java.util.properties info, class<?> caller) throws sqlexception initialize proper instance of java.sql.connection.

of course, can use similar approach in code use reflection to:

  • find proper implementation of interface
  • if there's proper implementation, create instance of class.
  • return instance of class once initialized , running.
  • throw proper exception(s) if class cannot found or if has initialization issue.

please not think creating jar containing interfaces , containing implementation classes of interfaces automatically wire on fly you, doesn't happen.


Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -