Solved

Interfaces

Posted on 1998-10-28
4
279 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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 18
split53 challenge 7 77
maven example error 3 52
simple java question 3 44
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

929 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now