Java doesn't have multiple inheritance, but you can implement multiple interfaces.
public class myApplet extends JApplet implements Serializable, SomeThingElse
You don't need to extend Object
since all classes will extend it.
If you're in the situation where you have methods in two classes you'd like to share in a single class, consider defining a third class which uses these two classes if the functions sets are cohesive and perform different operations. Don't try to combine classes to gain their powers. Create separate classes which tackle different problems and store instances of these in higher order classes.
If you do have two classes which perform similar functionality, it may be possible to abstract their common code into a superclass. So Class A would extend Class B, and you would in turn extend class C and add more specifics there.
If you have two classes which are very similar in structure but operate on different data structures or algorithms, then you might consider creating an interface which they both implement. A good example of this is a Vehicle interface which has a drive()
method. A Car
will have a different implementation to drive()
than a Motorbike
.
To extend this last idea, if these classes will share common code and it's possible to say "A Car is a Vehicle" and "A Motorbike is a Vehicle", then it's likely that using inheritance is preferred to an interface, with Car
and Motorbike
both extending Vehicle and it's abstracted functions.