Reputation: 11
I ran into a difficulty while doing an assignment for school. The problem is that the assignment requires us to create a class SummableSet that inherits from class IntSet, but I also need SummableSet to extend Applet in order to create an Applet that the Assignment also requires. I'm not sure how to go about doing this since I've done my research and it is not possible for a sub-class to inherit two classes. So how can I create an applet from these two classes if I can not extend Applet in my sub-class, SummableSet?
Upvotes: 1
Views: 294
Reputation: 285405
As you know, your class can only extend from one other class, and the solution is not to try to do the impossible. You'll need to create at least two classes here, one that extends Applet (why your teacher is having you use a dead technology like applets is beyond me), and the other new class that extends IntSet. Then the Applet extending class would use the other class in a "has-a" or "composition" relationship.
e.g..,
public class MyApplet extends Applet {
private SummableSet summableSet = new SummableSet();
@Override
public void init() {
// use summableSet here
}
}
public class SummableSet extends IntSet {
// ...... code for SummableSet here
}
Upvotes: 2