-4
  class A{} 
  class B extends A{} 

here is it the case that B is extending A alongwith object class.

  • We all know, in java, all classes by default inherits Object class.
  • But it is also there that multiple inheritance is not allowed in java.
  • Then when we inherit a class, what happens with this rule?
  • 2
    Inheriting from Object isn't multiple inheritance. It's single inheritance. – khelwood Jul 14 '20 at 13:04
  • Because the class you're inheriting from inherits from `Object`. – David Jul 14 '20 at 13:05
  • 5
    Multiple inheritance means a single class extending multiple classes, not a class extending a class which in turn extends a class. – Dave Newton Jul 14 '20 at 13:06
  • Cat inherits Mammal, which inherits Animal. One may say that Cat inherits Mammal and Cat also inherits Animal, which is true. Still, this is not multiple inheritance. – MC Emperor Jul 14 '20 at 13:07
  • `ArrayList` extends `AbstractList` which extends `AbstractCollection` which extends `Object`. One single inheritance chain. Everything other than Object itself has one direct superclass. – khelwood Jul 14 '20 at 13:08
  • @PRAJWALBHAGAT If you want to add code to your question, [edit] your question. Code in comments is really unclear. – khelwood Jul 14 '20 at 13:09
  • @PRAJWALBHAGAT Each class extends a single class. See my previous comment. – Dave Newton Jul 14 '20 at 13:10
  • You can use Java interfaces to have a sort of multiple inheritance. I find that I prefer this to having multiple inheritance with objects. – NomadMaker Jul 14 '20 at 13:13
  • `B` extends `A`. `A` extends `Object`. That is a single inheritance chain. – khelwood Jul 14 '20 at 13:21
  • Does this answer your question? [How does inheritance in Java work?](https://stackoverflow.com/questions/15367152/how-does-inheritance-in-java-work) – Tschallacka Jul 14 '20 at 13:28

1 Answers1

2

We all know, in java, all classes by default inherits Object class.

Yes. But I suspect that you don't understand what that really means.

What it means is that if a class is not declare (via an explicit extend) to inherit from some class, then it implicitly inherits from Object.

class A {}           // implicitly inherits Object

class B extends A {} // explicitly inherits A

To say this in other words:

  • A has Object as its only direct superclass
  • B has A as its only direct superclass.
  • B has Object as an indirect superclass.

This is single inheritance. Multiple inheritance would be if B had A and Object as direct superclasses.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216