Java API includes description of Object class as "Class
Object is the root of the class hierarchy.
Every class has Object as a superclass. All objects,
including arrays, implement the methods of this class."
So the question comes Is Object class implicitly inherited ?
API docs didn't say anything on this, documentation says the Object class is the root of the class hierarchy and every class has Object as a superclass .
It doesn't mean that Object class is inherited by every class , instead the behaviour is provided by compiler to make every class is a child of Object class.
Lets understand through code sample - source code A.java
class A {
public String toString(){
return "class A";
}
}
class B extends A {
public String toString() {
return "class B";
}
}
class C extends B {
public String toString(){
return "class C";
}
}
public String toString(){
return "class A";
}
}
class B extends A {
public String toString() {
return "class B";
}
}
class C extends B {
public String toString(){
return "class C";
}
}
Here I haven't extended Object class and still override toString() method of Object class.So does it mean compiler makes Object class inherited by class A and put it in byte code? Ahh...Need to dig into byte code.
No worry , will find out that too.
Here is the byte code - A.class
class A {
// Stack: 1, Locals: 1
A();
0 aload_0 [this]
1 invokespecial java.lang.Object() [8]
.........................
..........................
public java.lang.String toString();
0 ldc <String "class A"> [16]
2 areturn
Line numbers:
[pc: 0, line: 3]
Local variable table:
[pc: 0, pc: 3] local: this index: 0 type: A
}
B.Class
class B extends A {
// Method descriptor #6 ()V
// Stack: 1, Locals: 1
B();
0 aload_0 [this]
1 invokespecial A() [8]
// Stack: 1, Locals: 1
A();
0 aload_0 [this]
1 invokespecial java.lang.Object() [8]
.........................
..........................
public java.lang.String toString();
0 ldc <String "class A"> [16]
2 areturn
Line numbers:
[pc: 0, line: 3]
Local variable table:
[pc: 0, pc: 3] local: this index: 0 type: A
}
B.Class
class B extends A {
// Method descriptor #6 ()V
// Stack: 1, Locals: 1
B();
0 aload_0 [this]
1 invokespecial A() [8]
...............................
...............................
...............................
C.class
class C extends B {
// Method descriptor #6 ()V
// Stack: 1, Locals: 1
C();
0 aload_0 [this]
1 invokespecial B() [8]
// Method descriptor #6 ()V
// Stack: 1, Locals: 1
C();
0 aload_0 [this]
1 invokespecial B() [8]
..................................................
..................................................
Here , Compiler puts no-arg constructor by itself though I haven't included in source code. And there is no extends keyword in top most class A which says A extends Object , although there is a call to java.lang.Object() in A.class's byte code but it is a call to super class's constructor. very similar to what you can see in Class B and Class C 1 invokespecial A() [8] .
so the result is compiler won't add extends Object for any class and hence technically Object class is not inherited automatcially on the other hand since Object class's non private members are available to child classes(which is by definition inheriting already developed behaviour from parent class ) it is quite evident that Object class is inherited in Child class.
so the result is compiler won't add extends Object for any class and hence technically Object class is not inherited automatcially on the other hand since Object class's non private members are available to child classes(which is by definition inheriting already developed behaviour from parent class ) it is quite evident that Object class is inherited in Child class.
Does Object class break multiple inheritance ?
e.g. class A{ } class B extends A{ } , does it mean class B extends A , Object
No, reason Object class is the superclass in class hierarchy e.g. Object->A->B and not the direct superclass of all the classes.
So if Class A extends Object and Class B extends A , implicitly class B inheriting Class Object also.
So if Class A extends Object and Class B extends A , implicitly class B inheriting Class Object also.