aspose file tools*
The moose likes Java in General and the fly likes Calling Interface uder different package. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Calling Interface uder different package." Watch "Calling Interface uder different package." New topic
Author

Calling Interface uder different package.

Kevin P Smith
Ranch Hand

Joined: Feb 18, 2005
Posts: 362
Is it OK to call and Interface from a different package, or is this 'bad practice'?

I ask this because i have a simple database pooling project, which has the following structure:

+ com.myproject.connection
- ConnectionPoolmanager (well it's a DB connection pool manager)
- ConnectionManager (Uses ConnectionPoolManager to create/drop DB connecitons, returns Connection CONN)
- ConnectionInterface (interface to create/drop connection methods in Connectionmanger class)
+ com.myproject.sql
- SQLBuilder (builds SQL query based on fields, colums, table)

And basic code doing the following...
SQLBuilder.class:-
import com.myproject.connection.ConnectionInterface;
import com.myproject.connection.ConnectionManager;
...
ConnectionInterface Icon = new ConnectionManager();
...

My question is, what's the point of the Interface (or am I using it completely wrongly) because surely I can do teh exact same thing with the fllowing code:

import com.myproject.connection.ConnectionManager;
...
ConnectionManager Icon = new ConnectionManager();
...

Just I don't see the benifit of the Interface, it just seems to be another 'step' for the sake of it, I know it means there is no direct conneciton to the Connectionmanager class but still don't see the benifit.

Cheers
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Since you're certainly familiar with JDBC, take a few minutes and think about how JDBC works. Connection, Statement, ResultSet, and the like are all interfaces. Quick -- name some classes that implement these interfaces!

You can't, right? That's because, although each JDBC driver implements all these interfaces for you, the classes themselves are hidden behind factory methods like "getConnection()". Here's where the power of interfaces comes out: you can write code using only the interfaces, and it will work with many different implementations, without changing your code at all! Note the important point that your JDBC client code doesn't contain the names of any of the driver classes -- this is what makes the whole scheme work.


[Jess in Action][AskingGoodQuestions]
Kevin P Smith
Ranch Hand

Joined: Feb 18, 2005
Posts: 362
So am I using (understanding) the Interface correctly?

Is the purpose of the Interface really just to 'hide' my ConnectionManger (and in effect ConnectionPool manager) from my SQLBuid class?

import com.myproject.interfaces.*;
...
ConnectionInterface iconn = null;
...
iconn.getConnection();
[ October 01, 2007: Message edited by: Keith Seller ]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19672
    
  18

Originally posted by Ernest Friedman-Hill:
Since you're certainly familiar with JDBC, take a few minutes and think about how JDBC works. Connection, Statement, ResultSet, and the like are all interfaces. Quick -- name some classes that implement these interfaces!

com.mysql.jdbc.Connection
com.mysql.jdbc.Statement
com.mysql.jdbc.ResultSet


I only know these ones since I put the MySQL connector in my \lib\etc subfolder of my JRE, and Eclipse keeps wanting to import these instead of the interfaces.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Originally posted by Keith Seller:

Is the purpose of the Interface really just to 'hide' my ConnectionManger (and in effect ConnectionPool manager) from my SQLBuid class?


Yes, that's one important purpose of interfaces. This only makes sense, of course, if it's possible that at some point there will be more than one class that implements ConnectionInterface. If there will never be, then perhaps using an interface is unnecessary.

Of course, "never" is a dangerous word.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calling Interface uder different package.