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?
views:
276answers:
9No, 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
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.
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.
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)
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.
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.
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.
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.
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.