RdeLange
asked on
Inheritance
I have an object called database, i want to make children that inherit the connection.
Then I want the children to have methods as drop table etc.
But when I do this I have to write the same methods again and again.
How to solve this good?
DBMysQL connects to MysQL
DB400 connects to iSeries (not on the same way as mysql does)
DBMYSQLExtended
DB400Extended both must have methods as drop table etc.
How to solve?
Then I want the children to have methods as drop table etc.
But when I do this I have to write the same methods again and again.
How to solve this good?
DBMysQL connects to MysQL
DB400 connects to iSeries (not on the same way as mysql does)
DBMYSQLExtended
DB400Extended both must have methods as drop table etc.
How to solve?
Make the superclass have Connection getConnection() and implement the required methods in subclass
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
But of course you can limit the number of lines to write again and again,
by pulling out the common stuff as much as possible and do that in the Database class.
Example:
public class Database {
...
protected void dropTable(String tableName) throws SQLException {
// do all kind of local stuff (I mean calling Database methods)
try {
doExecuteQuery(getDropTabl eQuery());
} finally {
...
}
}
protected String getDropTableQuery(String tableName) {
return null;
}
...
}
public class DBMySQL extends Database {
...
// That's the only specific method needed to write for dropping a table
protected String getDropTableQuery(String tableName) {
return "drop table " + tableName;
}
...
}
public class DBExotic extends Database {
...
// That's the only specific method needed to write for dropping a table
protected String getDropTableQuery(String tableName) {
return "For table " + tableName + " doDrop"; // Some exotic way to drop a table
}
...
}
by pulling out the common stuff as much as possible and do that in the Database class.
Example:
public class Database {
...
protected void dropTable(String tableName) throws SQLException {
// do all kind of local stuff (I mean calling Database methods)
try {
doExecuteQuery(getDropTabl
} finally {
...
}
}
protected String getDropTableQuery(String tableName) {
return null;
}
...
}
public class DBMySQL extends Database {
...
// That's the only specific method needed to write for dropping a table
protected String getDropTableQuery(String tableName) {
return "drop table " + tableName;
}
...
}
public class DBExotic extends Database {
...
// That's the only specific method needed to write for dropping a table
protected String getDropTableQuery(String tableName) {
return "For table " + tableName + " doDrop"; // Some exotic way to drop a table
}
...
}
I suggest: Delete and no refund.
I consider my comments as relevant and worth to PAQ