Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does every class extend Object? [closed]

I read that every class extends a object class by default. Then it should cause multiple inheritance and must not be supported by Java.

like image 246
user2848012 Avatar asked Sep 13 '26 18:09

user2848012


2 Answers

When you have something like

class A {}

then A implicitly extends Object. In fact the bytecode resembles

class A extends Object {}

Now, if we have

class B extends A {}

Then B extends A but is also a subclass of Object, since A is a subclass of Object. This is not, however, multiple inheritance:

Object
  |
  A
  |
  B

Multiple inheritance would look like this:

Object   A
  \     /
   \   /
    \ /
     B

i.e. B inheriting from two hierarchically unrelated classes.

like image 66
arshajii Avatar answered Sep 16 '26 07:09

arshajii


How can it be multiple inheritance?

class Object { /* stuff */ }

class Foo /* implicit extends Object */ {}

class FooBar extends Foo /* and therefore extends Object */ {}

The rules are described in the Java Language Specification:

The class Object is a superclass (§8.1.4) of all other classes.

like image 35
Sotirios Delimanolis Avatar answered Sep 16 '26 08:09

Sotirios Delimanolis