-4

For eg: Case 1: Does every Java class we create will extend Object class and if so then how come we an able to extend one more class to our class may be silly question just got it in mind i am posting this question

Chigulla
  • 39
  • 3

4 Answers4

3

Pretty simple: The hierarchy of java-classes is tree-like, with Object as root. If a class doesn't extend any other class it directly extends Object by default. So what about classes that extend some other class, like class A extends B. Now A extends B and Object. But this is not multiple inheritance. A extends B, thus A aswell extends any superclass of B, including Object. Just imagine the inheritance this way:

                       Object
                      /   |   \
                     B   ...   SomeObject
                     |
                     A
  • I think talking about a tree is confusing in this case. What we are interested in is the parent classes of a given class (they are literally the classes we inherit from), ant that's never a tree in Java – Dici Aug 18 '15 at 20:46
  • 1
    well, if you want to look at it this way... . Nevertheless the overall structure is a tree. And for multiple-inheritance, even if we look at the relationship between superclasses of a given class, it's still a graph. Just assume the following relationship: `D`, `C extends D`, `B extends D`, `A extends B,C`. That's no tree, but still a valid inheritance-relationship in a language allowing multiple inheritance –  Aug 18 '15 at 21:09
  • Mmm that's true, parents can form a graph for multiple inheritance – Dici Aug 18 '15 at 21:13
2

Yes every class extends Object. However, Object is not necessarily the direct superclass. If a class Child extends Parent, then Child's direct superclass is Parent. In this case, the class hierarchy is:

Child < Parent < Object

So there is no multiple inheritance for Child. Object is inherited transitively by its direct superclass Parent.

M A
  • 71,713
  • 13
  • 134
  • 174
1

No offense, but your english makes your question very difficult to read. I think I understood it though. Multiple inheritance is not the contrary of inheriting only from one class. You can inherit from several classes and still not have multiple inheritance.

For example, C inherit from A and B if A extends B, B extends C. However, the inheritance is a linear chain here, whereas multiple inheritance allows for trees, for example A extends B, A extends C. This is different, as you see. In java, you can inherit from as many classes as you like by transitivity but directly extend only one class.

Dici
  • 25,226
  • 7
  • 41
  • 82
  • normal inheritance allows trees. multiple inheritance actually allows graph-like inheritance-chains –  Aug 18 '15 at 20:43
  • 1
    ...the diamond of death! – ChiefTwoPencils Aug 18 '15 at 20:43
  • The ancestors of a node (in your tree) will always be a linear chain, not a tree. See my comment on your answer. You are right about the graph and all, but I find it clearer to talk about the linearity of the ancestors than to consider other unrelated classes which form a tree together – Dici Aug 18 '15 at 20:47
1

Multiple inheritance is when you try to inherit more than a class at the same time,

I mean like

class Foo extends Faa, Fii {
}

that is not posible in Java but we can have this

class Faa{
} 

class Fii extends Faa{
}

class Foo extends Fii{

}

that is not the same, in that case we have 2 parents (i mean Fii because we are extending from Fii and Faa because Fii extends from it)

jorgevasquezang
  • 1,008
  • 1
  • 9
  • 16