Solved

Interfaces

Posted on 1998-10-28
4
283 Views
Last Modified: 2006-11-17
Hi
              I am new to Java.

              I am trying to understand interfaces.
              I understand that an interface is a way to replace multiple
              inheritance(C++). I understand that an interface is just a set of
              static final variables and methods that are just declared. The
              implementation is done in the class that implements the interface.
              I can not declare an object of the interface type, but I can use a
              method of the class that implements the interface. Is my
              understanding OK up to here ?
              Now, I am playing with JDBC and there is there a group of
              interfaces
              :Connection, Statement, DataBaseMetaData, PreparedStatement,
              CallableStatement, Driver and DriverManager.
              Well I understand that the thing is going this way:
              Driver driver =
              (Driver)Class.forName(driverName).newInstance();
              Connect connect = driver.connect(dbURL, p);
              DataBasaMetaData db = connect.getMetaData();
              Well, it looks that the interfaces returning other interfaces.
              Which is the class that implements them ?

              Thank you.



       
0
Comment
Question by:simi
  • 2
4 Comments
 

Accepted Solution

by:
seena earned 50 total points
ID: 1226564
Simi,

  Before these type of JDBC implementations u will be importing some library files i trust the classes might be reside on those files.  Regarding interfaces will u please explain it in more detail.  Normally the working of a java code willbe the same as the C++ code expect some upcasting stuff and the downcasting stuff.  Please explain us in detail abt the interface what ur exactly trying to ask.

Thanks in advance,
Seena
0
 
LVL 27

Expert Comment

by:BigRat
ID: 1226565
If the Rat may add twopence worth of cheese.
   An interface in Java may be considered as a collection of data and methods which any conforming class must provide. In JDBC the Connection Interface requires that any class implementing it provide those procedures (called methods). If I have a database, lets call it CheeseBank, and I want to make my database available for Java programmers I must provide a class which implements the Connection interface :-

     public class CheeseConnection extends RatsBank implements Connection {.....
     ....}

   When the DriverManager loads Rat's JDBC Driver and you ask for a Connection you actually get the CheeseConnection class returned. Or strictly speaking an object instantiated from this class. Since CheeseConnection implements all of the requirements of Connection it may be considered as a Connection object, although strictly speaking there are no such things. This is called polymorphism and JDBC is a good example of the "Factory" method in object orientated programming. More information on such things can be found in every good rats bookshelf :-

    Design Patterns, Gamma et al. Addison Weseley
 ISBN 0-201-63361-2
0
 

Author Comment

by:simi
ID: 1226566
I got some more understanding of it.
Still not all.
I will post the whole code:
import java.awt.*;
import java.util.*;
import java.sql.*;

public class MaxExample extends Frame
{
   public MaxExample()
   {
   super();
   this.resize(300, 300);
   this.show();

   TextArea t = new TextArea(25, 80);
   this.add("Center", t);

   String driverName = "symantec.itools.db.jdbc.Driver";
   Driver driver;
   Connection connection;
      String dbURL = jdbc:dbaw://localholst:8889/Symantec_SQLANY/My_database/My_database";
   Properties p = new Properties();
   p.put("user", "dba");
   p.put("password", "sql");
   try{
      driver = (Driver)Class.forName(driverName).newInstance();
      connection = driver.connect(dbURL, p);
      DatabaseMetaData db = connection.getmetaData();
      String s = "";
      s += Integer.toString (db.getMaxColumnsInSelect());
      s += Integer.toString(db.getMaxTablesInSelect());
      s += Integer.toString(db.getmaxColumnsInTable());
      t.setText(s);
      }
      catch(SQLException e){}
      catch(Exception e){}
   }
   public static void main(String args[])
   {
      MaxExample app = new MaxExample();
   }
}

Driver is an interface.
By calling Class.forName(driverName).newInstance(); I am instanciating an object from a class defined somewhere in the
java.sql package. This class is actually supposed to be implementing the Driver interface. Is my understanding right here ?

Then I am using this instance, the driver object, by calling a method , conection that returns me a Connection object.
But Connection is also an interface. That means that the object that I am talking about also implements the Connection methods.
In this case, all interfaces are implemented somewhere in the
class contained in the imported "import.java.sql.*;".
I am instantiating an object through my Class.forName..., and this object returns another object whose method called returns another object and so on.

Did I get it right ?

Thank you.
0
 
LVL 27

Expert Comment

by:BigRat
ID: 1226567
Yes it is all rather confusing isn't it? The real key is the statement
  driver = (Driver)Class.forName(driverName).newInstance()

The driverName string is used as a "filename" to access a class in this case symantec.itools.db.jdbc.Driver which was wriiten by Symantec. The fact that the Symantec class is called Driver is unfortunate and also irrelevant. It could just as well be called Fred, in which case your diverName string would be symantec.itools.db.jdbc.Fred. The definition of this class would then be :-
    public class Fred implements java.sql.Driver {......

Since Fred implements Driver it can stand in place of Driver. Te result from the newinstance() call of Class.forName is an object whhich, in this case, implements Driver. Hence the "cast" :-
     (Driver)Class.forName(....).(..);
   I do recomend either downloading the JDBC spec from Sun or loaning a book on jdbc from your local library. I got "JDBC database Access with Java" From AddisonWesley ISBN 0-201-30995-5 last XMas!
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Application launch issue with Apache Tomcat 5 45
JUnit 4 @Before and @BeforeClass differences 3 60
use lov values 2 50
pagenation logic how it is working in my code 1 31
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This video teaches viewers about errors in exception handling.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question