It's not a secret anymore!*
The moose likes Beginning Java and the fly likes Java Class Implementation Logic Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java Class Implementation Logic" Watch "Java Class Implementation Logic" New topic
Author

Java Class Implementation Logic

ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
In some classes of Java .We cant make object like this :
ClassName obj = new ClassName();
like Connection , ContentPane .
Instead of that they have a method for making object like : getConnection(----),getContentPane(----) .
I just want to know the logic behind this approach . What is the advantage .
Why some class has such type of implementation & why not others .

Thanks .
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

If by connection you mean a java.sql.Connection, it is an interface not a class. You can't instantiate an interface. But you can instantiate a class which implements an interface - so the getConnection method is beahving as a factory: it is instantiating the correct class which implements the Conneciton interface specific to whichever JDBC driver you are using.

I'm not aware of the ContentPane class - where did you get it?
[ December 15, 2004: Message edited by: Paul Sturrock ]

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
OK , I am sorry about Connection Interface .

One more thing in this also , why they made this as a Interface . It doesn't force us to implement any method ( bcoz we dont implements that interface ) . It is also not a markable interface ( or is it ) .
we just make a reference of it & with getConnection(--) method we make an object & assign to this reference .

Why not they made it a class :
Connection con = new Connection(---);
& then use con anywhere .

Plz post ur reply ASAP .
thanks .
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

A connection to a database server is always going to be full of platform-specific code. The only way you could create a class which handles this would be with one almighty conditional. It would also require Sun to write all the connection logic for every database which wants to support JDBC. Instead Sun defined a Connection as an interface, and made it a requirement for any database vendor who want's to say their database supports JDBC to implement the Connection object. We don't (necessarily) implement this interface, but JDBC driver writers do.

Think about what writing this would require:

It means that you have hard-coded that your program works with Oracle only. And quite probably you have also hard-coded one particular Connection implementation. Suppost instead we write:

We are asking the DriverManager to find any avaliable Connection implementation which will allow us to connect to the database which uses the url named. So without changing any code I can swap between an Oracle 8 driver and an Oracle 10 driver.

Of course if you use the documented prefered connection route and let a DataSourdce encapsulate everything, then you don't even have db URLs in code.
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
You r too good in JDBC paul . Thanks . Do you know all the interface logic ( like why this is an interface ) .
Bye , god bless you .
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1817

The Connection is an interface because that enables you, the standard programmer, to not have to worry about implementation details. Each database vendor (Oracle, SQL Server, MySQL, etc.) will have their own version of the Connection class (OracleConnection, SQLServerConnection, MySQLConnection, etc. [NOTE: Those aren't the real names, but they will serve as an example...]).

Now you, writing a JDBC program, want to connect to a database. If it's a well-designed OOP program, you are not going to be tied to a specific database -- you don't care what database it is, they should all work the same. (This is called being "loosly coupled", or "decoupled"). As such, you don't want database specific code in your classes. If you used the OracleConnection, then you wouldn't be able to connect to MySQL.

This is where the interface comes in. You just ask for a Connection from the DriverManager and it returns you the Connection. Since the class implements the interface, you know that you can call methods like prepareStatement and isClosed -- all Connections must implement those functions.


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
 
 
subject: Java Class Implementation Logic
 
Similar Threads
object
why is the method not getting executed??
Proxying Objects Causes OutOfMemoryError
about hashcode()
why we exteds object