Link to home
Start Free TrialLog in
Avatar of RdeLange
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?
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Make the superclass have Connection getConnection() and implement the required methods in subclass
ASKER CERTIFIED SOLUTION
Avatar of zzynx
zzynx
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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(getDropTableQuery());
        } 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
    }
     
    ...
}
Avatar of Giant2
Giant2

I suggest: Delete and no refund.
I consider my comments as relevant and worth to PAQ