== JAVA and multi inheritance ==
4 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   joey_corleon
Posted On:   Tuesday, June 24, 2003 04:00 PM

Hi all,


I am stuck in my project as I would really need to find a way to implement multi-inheritance in JAVA :o(


Does anyone know some sort of workaround tip to get the same behavior?



Thanks in advance!

Re: == JAVA and multi inheritance ==

Posted By:   Anonymous  
Posted On:   Thursday, June 26, 2003 02:21 AM

As multiple inheritance is impossible in Java, you have to find other ways
to solve your problem.

Composition is obviously one of these ways. It deserve all your attention. Composition
is actually the usual way to sort out hierarchies problems. Do not neglect it.


If the reason why you need multiple inheritance is because of polymorphism.
You should have a look at Interfaces. Interfaces are the way to manipulate objects
coming from different branches of your hierarchy. They also provide the ability
to have some kind of multiple inheritance as a class can implement more than
one Interface and as Interfaces are treated as types in Java.


I have written a simple article about this question recently. So, if this is
your current problem, you can have a look:


www.liemur.com/Articles/PolymorphismAndJavaInterfaces.html


Regards.


S.L.

www.liemur.com

Re: == JAVA and multi inheritance ==

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.

Re: == JAVA and multi inheritance ==

Posted By:   Anonymous  
Posted On:   Wednesday, June 25, 2003 02:00 AM

Don't look for multiple inheritance in Java. It is impossible.



On the other hand, depending on the reason why you want multiple inheritance there are some "ways".

In fact if you are looking for implementing polymorphic behaviour in Java, even if objects are not coming from the same branche, you can use Interfaces.



I have recently written a document about this. If you want to have a look:



http://www.facian.com/Articles/PolymorphismAndJavaInterfaces.html



Regards.



S.L.

www.liemur.com

Re: == JAVA and multi inheritance ==

Posted By:   Simon_Ablett  
Posted On:   Wednesday, June 25, 2003 01:30 AM

You can't implement multiple-inheritance in Java.

Regards.
About | Sitemap | Contact