Solved

Interfaces

Posted on 1998-10-28
4
295 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What's wrong with this code? 4 46
Which non-HTML GUI front end to use with Java? 3 67
Delete .class files at every compile 4 57
Session in java desktop 5 37
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
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.
Suggested Courses

738 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