Solved

Interfaces

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

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…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

749 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