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.
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
> 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.
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
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