Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to have a class with more than one superclass?

I am creating an incredibly primitive blackjack game for my high school programming class using Swing, therefore, all my resource classes extend JPanel. I created the following resource classes:

  • DealerBox
  • ClubsBox
  • SpadesBox
  • HeartsBox

My problem is that I want to create a handful of public integers such that I have

public int DealerTotal, ClubsTotal, SpadesTotal, HeartsTotal;

I want to do this so I can track the values of everybody's hands and determine winners and losers. I had a lightbulb go off and thought that making the Dealerbox a superclass to Clubs, Spades, and Hearts was the solution to all my troubles as I could have simple code in the superclass getting values for the above integers and do all the checking I need.

So I wrote this for one of my subclasses:

public class ClubsBox extends JPanel, DealerBox implements Runnable
{
//fun code 
}

The problem now is that I keep getting an error. I assumed it had something to do with listing ClubsBox as being the subclass of JPanel AND DealerBox. So I stackoverflowed my problem and found an answer that stated that one cannot have a class that has 2 superclasses. Another answer to a similar question said that one COULD have a class that has two superclasses.

Now we get to the main question: Is it possible to have a class that has two superclasses? If so, how do I code it? If not, is there a way around the restriction?

EDIT: 21 APRIL 2014, 44 MINUTES AFTER ORIGINAL POST

I have a new problem. I used the method that everyone suggested which was to make DealerBox extend JPanel then to have everything else extend DealerBox. My GUI went from this: GUI before chaining

To This:

GUI after chaining The bottom one is clearly incredibly screwed up. Somehow labels from other areas ended up being copied and stuff got mixed around. If you need the code let me know, but I need serious help fixing this.

Please keep in mind the inheritance tree looks like this:

InhertianceTree

like image 767
Ungeheuer Avatar asked Dec 07 '22 02:12

Ungeheuer


2 Answers

Unfortunately you can't have more than one direct superclass in Java. But you can chain them together like this:

public class DealerBox extends JPanel
public class ClubsBox extends DealerBox

Now with the new issue of your components not being laid out in the same manner as they were before, it's too hard to guess at what's going on without seeing some code. Are you putting common code into the constructor for DealerBox, and calling it from each DealerBox subclass? Like so:

public class DealerBox extends JPanel {
    public DealerBox(String info) {
        super(); // JPanel constructor
        doSomething();
    }
}

public class ClubsBox extends DealerBox {
    public ClubsBox() {
        super("Clubs"); // DealerBox constructor
    }
}
like image 89
SimonT Avatar answered Dec 08 '22 14:12

SimonT


Quote

"When Sun was designing Java, it omitted multiple inheritance - or more precisely multiple implementation inheritance - on purpose. Yet multiple inheritance can be useful, particularly when the potential ancestors of a class have orthogonal concerns. This article presents a utility class that not only allows multiple inheritance to be simulated, but also has other far-reaching applications. Have you ever found yourself wanting to write something similar to:

public class Employee extends Person, Employment {
// detail omitted
}

Here, Person is a concrete class that represents a person, while Employment is another concrete class that represents the details of a person who is employed. If you could only put them together, you would have everything necessary to define and implement an Employee class. Except in Java - you can't. Inheriting implementation from more than one superclass - multiple implementation inheritance - is not a feature of the language. Java allows a class to have a single superclass and no more."

One option to you is "Inheritance from Inheritance":

public class Person extends Employee
public class Employee extends Employment
like image 32
Victor R. Oliveira Avatar answered Dec 08 '22 15:12

Victor R. Oliveira