Java Reference
In-Depth Information
Notice that each of the DB classes is defined as abstract and extends DB. Because they are abstract, they do not
have to implement the abstract methods defined in DB. However, all of the abstract methods will have to be defined
in Employee and the ones that are currently defined as
private
will have to have the access modifier removed.
Because DB defines the methods as
protected
, Employee can't define them more restrictively as
private
. Speaking
of changes to Employee, Employee must call the init method (to establish the connection to the database) and we
need to implement a getFieldsFromRS method. We could rename getEmpInfo-FromRS to getFieldsFromRS, however,
it's simpler to code getFieldsFromRS to invoke get-EmpInfoFromRS. Finally, of course, you would substitute all your
connection information for the example information above.
In Employee, we would change the constructor to invoke init as follows:
public
Employee(){
init();
}
If there are multiple constructors, all must invoke the default constructor. In Employee, there is a constructor that
accepts five strings. It would be modified to the following:
public
Employee(String name, String street, String
city, String state, String zip) {
this
();
this
.setEmpName(name);
this
.setEmpStreet(street);
this
.setEmpCity(city);
this
.setEmpState(state);
this
.setEmpZip(zip);
}
As mentioned, each of the
private
methods headers must be changed as follows:
String createValuesClause() {...
String createSetClause() {...
String createWhereClause() {...
void
doSelect(String select) {...
void
doUpdate(String update) {..
and getFieldsFromRS can be coded as follows:
void
getFieldsFromRS() {
getEmpInfoFromRS();
}
Assuming Employee is currently in the DB2 database, the Employee class header would look like the following:
public class
Employee
extends
DB2DB{...
and the class diagram would look like Figure
10-13
. Now comes the magic of inheritance.