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?

- 1,082,665
- 372
- 3,610
- 3,555

- 4,915
- 3
- 20
- 10
8 Answers
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.

- 83,943
- 34
- 151
- 241
-
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 Aug 10 '10 at 12:36
-
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 Aug 10 '10 at 12:38
-
Exactly. You can still have Mom, Dad, Weekend Dad, etc. Lots of different possibilities for real-world analogies. – Shawn D. Aug 10 '10 at 12:42
-
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 Aug 10 '10 at 12:55
-
Note that the term "parent class" is also not used in the Java Language Specification; it's called a "direct superclass" there. – Jesper Aug 11 '10 at 10:10
-
@Jesper: Ok, true enough, but the metaphor still holds well enough to illustrate the multiple superclass/single direct superclass distinction. – recursive Aug 11 '10 at 13:05
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.

- 27,197
- 9
- 43
- 57
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

- 167,322
- 27
- 342
- 338
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)

- 4,538
- 9
- 25
- 25
-
5
-
3I'm surprised Java got to where it was without a convenient way to represent donkeycars... – Andrzej Doyle Aug 10 '10 at 12:35
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.

- 4,327
- 5
- 35
- 52
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.

- 30,365
- 9
- 60
- 95
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.

- 308
- 2
- 6
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.

- 16,947
- 4
- 41
- 53