File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Class Implementation Logic

 
ankur rathi
Ranch Hand
Posts: 3830
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
ankur rathi
Ranch Hand
Posts: 3830
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3830
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1824
Eclipse IDE Java Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic