Forums > Java

multiple class inheritance

July 4, 2008 | 7:57 pm

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..///


July 4, 2008 | 8:16 pm

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


July 4, 2008 | 8:26 pm

> 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.


July 4, 2008 | 8:56 pm

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.


Viewing 4 posts - 1 through 4 (of 4 total)