0

Possible Duplicate:
Alternative of Multiple inheritance in Java
Multiple inheritance in Java?

i have a code like so:

public class myApplet extends JApplet

im interested in adding another extend like so:

public class myApplet extends JApplet, extends Object implements Serializable

can this be done some way and how?

Community
  • 1
  • 1
user2010884
  • 58
  • 1
  • 10

6 Answers6

1

Java doesn't have multiple inheritance, and any class extends Object anyway, so it's useless in that case.

Frank Pavageau
  • 11,477
  • 1
  • 43
  • 53
1

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.

Aram Kocharyan
  • 20,165
  • 11
  • 81
  • 96
1

you can not extend more than one class in Java. But you may implement more than one interfaces.

However, if your parent is a child from another parent you are of course also extending all classes in the hierarchy.

If you do not add a extend-statement, Object is automatically choosen as parent. Object is the super-parent, and in every hierarchy you will find it. Therefore in your case you can skip the "extends Object" as the hirachy is the following:

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Container
          extended by java.awt.Panel
              extended by java.applet.Applet
                  extended by javax.swing.JApplet
                      extended by your.package.myApplet

(see http://docs.oracle.com/javase/6/docs/api/javax/swing/JApplet.html)

0

You can't extend class twice in java... Nevertheless every class in java inherits from class Object instantly, so you don't have to extend your class with Object class ;)

Martin Dvoracek
  • 1,714
  • 6
  • 27
  • 55
0

No, Java doesn't support multiple inheritance. Every class inherits from Object automatically though.

Michiel Borkent
  • 34,228
  • 15
  • 86
  • 149
0

Java doesn't support multiple inheritance,you can use multiple interface

Afsun Khammadli
  • 2,048
  • 4
  • 18
  • 28