views:

276

answers:

9

Class Object is the root of class hierarchy. Every class has Object as a superclass. So, if I am extending a API class, will it be like, multiple inheritance? Obviously, Java doesn't support multiple inheritance. How does it then work?

+4  A: 

No, Object will just be the eventual parent class of any class you create

Multiple inheritence would mean you could write a class that extends String and Integer for example, and gains the properties of each. This cannot be done with Java. You probably want to look at the Delegate pattern if this is the sort of thing you want to do

tim_yates
+9  A: 

Superclass is not the same thing as parent class. You can only have one mother, but you have a much larger number of female ancestors.

recursive
But in C++, which does support multiple inheritance, you can have multiple mothers? The meaning of the word "inheritance" in object oriented programming is very different from the biological meaning; direct analogies don't work.
Jesper
Yes, in C++, you can have multiple mothers, where the mother relationship is an analogy to the parent class relationship. I don't see what's wrong with the analogy. I mean, it's built into the term "parent class".
recursive
Exactly. You can still have Mom, Dad, Weekend Dad, etc. Lots of different possibilities for real-world analogies.
Shawn D.
The term "parent class" is also wrong because it confuses biological inheritance with object oriented programming inheritance. In OO, a subclass instance *is an* instance of the superclass (Liskov substitution principle). Calling the superclass "Parent" and the subclass "Child" means you're saying: "A Child is a Parent". That makes no sense.
Jesper
Note that the term "parent class" is also not used in the Java Language Specification; it's called a "direct superclass" there.
Jesper
@Jesper: Ok, true enough, but the metaphor still holds well enough to illustrate the multiple superclass/single direct superclass distinction.
recursive
+2  A: 

The super class of your object also has a super class and so on.

All objects form a tree with java.lang.Object as it's root node.

Stroboskop
+3  A: 

no, its just inheritance, business as usual

grandparent

parent

child

the child only has one parent, and the parent has a grandparent (doesnt make logical sense, but whatever :)

multiple inheritance would be when you inherit from two different classes does not need to have anying do with each other

donkey      car

     donkeycar

(as you already noted, its not possible in java)

JohnSmith
I really want a donkeycar now... ;-)
tim_yates
I'm surprised Java got to where it was without a convenient way to represent donkeycars...
Andrzej Doyle
+1  A: 

No.

The multiple inheritance mean that You inherit for example from two classes

class A {}

class B {}

class C extends A, B {}

and this is not possible in Java.

What You can do is

class A {}

class B extends A {}

class C extends B {}

So You have more then one super class but only one parent.

Vash
A: 

My understanding of it it that multiple inheritance works horizontally (multiple parent superclasses inherited directly into one subclass) rather than vertically (parents of parents), thinking of the inheritance tree.

As you say, only the vertical kind is allowed in Java.

Enigmaster
A: 

Multiple inheritance is omitted by Sun Microsystem. However, multiple inheritance is useful in some way. Here is a link that might help you out with java multiple inheritance.

http://java.sys-con.com/node/37748

Hope that helps.

samer
+4  A: 

Java doesn't support multiple inheritance, as everyone else explained.
But you can (kind of) have multiple inheritance when you implement multiple interfaces:

interface Moveable {
    void relocate(Coordinate position);
    Coordinate getCurrentPos();
}

interface Tradeable {
    void sell(BigInteger amount);
    void buy(BigInteger amount);
}

interface Crashable {
    void crash();
}

class Vehicle implements Moveable, Tradeable, Crashable {

}

Now Vehicle should all methods from the interfaces it implements.

quantumSoup
A: 

Inheritance is transitive. You extend an API class, and that API class itself extends Object. So you're indirectly extending Object. Shorter, if A extends B and B extends C, then A extends C.

This is not multiple inheritance. The 'inheritance chain', if you will, can be as long as you want it to be. But in the end, everything has Object at the end of it.

Real multiple inheritance would be extending from two unrelated classes at once. You cannot do this in Java.

Joeri Hendrickx