Posted By:
David_Bates
Posted On:
Wednesday, June 25, 2003 06:21 AM
I think we've now established that Java doesn't support multiple-inhertiance!
We might be able to give a better answer if you outline why you're needing it.
I would say that as a general rule, you shouldn't need multiple inheritance. If you think you need it, re-evaluate your inheritance hierarchy, there are probably some problems in there somewhere.
Generally, you should be use "composition" as a solution. For example, you might think you need multiple-inhertiance so that you can create a dialog which allows you to edit Employee details (I'm assuming Java could cope with a form of multiple inheritance):
public class Employee {
protected String name;
public Employee(String name) {
this.name = name;
}
public void writeToDatabase() {
// writes the employee to the database
}
}
public class Window {
public Window(Rectangle dimensions) {
setSize(dimensions);
}
public void paint() {
// Draws the window
}
}
public class EmployeeWindow extends Employee, Window {
public EmployeeWindow(String name, Rectangle dimensions) {
Employee.super(name);
Window.super(dimensions);
addButton(new Button("Save") {
actionPerformed(ActionEvent e) {
writeToDatabase();
close();
}
});
}
...
}
This allows you to have a class which can display an employee graphically and also save itself to the database. However, this is bad because the RESPONSIBILITIES of the object are blurred. Instead, composition could be used:
public class Employee {
protected String name;
public Employee(String name) {
this.name = name;
}
public void writeToDatabase() {
// writes the employee to the database
}
}
public class Window {
public Window(Rectangle dimensions) {
setSize(dimensions);
}
public void paint() {
// Draws the window
}
}
public class EmployeeWindow extends Window {
private Employee employee;
public EmployeeWindow(Employee e, Rectangle dimensions) {
super(dimensions);
this.employee = e;
addButton(new Button("Save") {
actionPerformed(ActionEvent e) {
employee.writeToDatabase();
close();
}
});
}
...
}
This is the better solution. An EmployeeWindow IS-A type of window. An EmployeeWindow HAS-A Employee object which it displays. Functionality is still in tact!
Hope this helps,
David.