Solved

Interfaces

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
factorial example challenge 10 62
json example 39 115
pairs challenge 5 45
Running Jira on Raspberry PI 2? 3 137
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…
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…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

747 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

14 Experts available now in Live!

Get 1:1 Help Now