A question about Inheritance

Hi,

I have created a class which implements java.sql.Connection.

class newConnnectionimplements Connection{

...

close(){

freeConnection();

}

...

}

In my main class I am assigning newConnection object to a Connection object because I want to use the Connection's close method to actually close the connection.

main{

newConnection newConn = newConnection();

Connection conn = (Connection)newConn

conn.close();

}

But it doesn't use the Connection class method. It uses the newConnection class method. Is there any way I can make use of the Connection method close?

Thanks.

[897 byte] By [bce_developera] at [2007-11-27 3:45:19]
# 1
I don't understand this question.What are you trying to accomplish?
xiarcela at 2007-7-12 8:49:01 > top of Java-index,Java Essentials,Java Programming...
# 2
I want to use the Connection class method 'close' rather than the newConnection class method 'close'
bce_developera at 2007-7-12 8:49:01 > top of Java-index,Java Essentials,Java Programming...
# 3

Connection only declares the close method, it doesn't provide any implementation. And even if it did, casting wouldn't be the way to do it. The object in memory is the same object, and has the same methods, regardless of how you're referring to it. You need to get hold of the actual Connection implementation as provided by your JDBC vendor

georgemca at 2007-7-12 8:49:01 > top of Java-index,Java Essentials,Java Programming...
# 4
You don't want to use the newConnection class's close() method? Then don't use a newConnection object.
DrClapa at 2007-7-12 8:49:01 > top of Java-index,Java Essentials,Java Programming...
# 5

> I want to use the Connection class method 'close'

> rather than the newConnection class method 'close'

Either have the NewConnection.close() method call super.close(), or leave out the close method in NewConnection entirely. Then when you call close on the newConnection, the connection's version is called. If you want to expose 2 different ways to close the connection, one the NewConnection way, and one the old Connection way, then create two different methods.

hunter9000a at 2007-7-12 8:49:01 > top of Java-index,Java Essentials,Java Programming...
# 6
> I have created a class which implements java.sql.Connection.Really? Doesn't that interface have nearly 50 methods? I'm wondering because I've never needed to do that. Why did you need to do that?
DrLaszloJamfa at 2007-7-12 8:49:01 > top of Java-index,Java Essentials,Java Programming...
# 7

Its stupid why I did it. And now if I take out that method it'll effect too many things. I was creating a connection pool class. I created a newConnection class because I need to implement some methods which communicated with my ConnectionPool class (keep tracking of open connections, leasing connections, returning connections back to the pool, etc). The close method was actually calling another method to free the connection and return it back to the pool. I guess what I will do is just create a new method that actually closes the connection by calling the Connection.close method.

Thanks guys

bce_developera at 2007-7-12 8:49:01 > top of Java-index,Java Essentials,Java Programming...
# 8
There are lots of free third party connection pool implementations.Did you consider any of them? I'm a big fan of not reinventing the wheel: http://java-source.net/open-source/connection-poolsC3P0 and DBCP may be the most popular.
DrLaszloJamfa at 2007-7-12 8:49:01 > top of Java-index,Java Essentials,Java Programming...
# 9

But here's what I'm actually doing:

I think that there is still a way I can use the Connection object.

// Close all connections in the connection list

public void closeConnections(Vector connList) throws SQLException{

Enumeration connections = connList.elements();

while(connections != null && connections.hasMoreElements()){

Connection connection = (Connection)connections.nextElement();

if(!connection.isClosed())

connection.close();

}

}

connections contains objects but they are the newConnection objects. So when they get assigned to connection, connection actually turns into newConnection and ends up calling the newConnection.close() method

bce_developera at 2007-7-12 8:49:01 > top of Java-index,Java Essentials,Java Programming...