multiple class inheritance

Jul 4, 2008 at 7:57pm

multiple class inheritance

Hello there !

I’m trying to make :
public class antsimulation extends MaxObject extends SimState {

In Java, I can only do :
public class antsimulation extends MaxObject {

“extends SimState” is not allowed… I have tryed something like
“implements SimState” but it does not work.

O..///

#38743
Jul 4, 2008 at 8:16pm

On 4 juil. 08, at 21:57, olivier pasquet wrote:

> Hello there !
>
> I’m trying to make :
> public class antsimulation extends MaxObject extends SimState {
>
> In Java, I can only do :
> public class antsimulation extends MaxObject {
>
> “extends SimState” is not allowed… I have tryed something like
> “implements SimState” but it does not work.

As far as I remember a class can only herite from one class. You
probably want to use an interface.

ej

#135334
Jul 4, 2008 at 8:26pm

> As far as I remember a class can only herite from one class. You
probably want to use an interface.
< yes, indeed. cheers.

I’m trying to make :
public class antsimulation extends MaxObject extends SimState {

In Java, I can only do :
public class antsimulation extends MaxObject {

Instead, you should use :
public class antsimulation extends MaxObject {
public interface SimState{

That’s all.

#135335
Jul 4, 2008 at 8:56pm

Yup, no multiple inheritance in Java. Interfaces are the standard alternative, but you could end up implementing the same interface the same way in multiple places. Redundant code is very bad…

So keep in mind inheritance is not the only way to share code between classes. By composing objects (just stick one object inside another as a member variable), you can get many of the same benefits of multiple inheritance. Google for “composition vs inheritance” if the idea isn’t clear.

#135336

You must be logged in to reply to this topic.