aspose file tools*
The moose likes Java in General and the fly likes All classes extend java.lang.Object. But how does this work? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "All classes extend java.lang.Object. But how does this work?" Watch "All classes extend java.lang.Object. But how does this work?" New topic
Author

All classes extend java.lang.Object. But how does this work?

Rajkamal Pillai
Ranch Hand

Joined: Mar 02, 2005
Posts: 443
    
    1

This is a pretty elementary question.

The language specification says that all Class (es) extend java.lang.Object even if they explicitly mention extends Object or otherwise. I am interested in understanding how this works?

If a Class does not extend from any other then the compiler creates the Object hierarchy such that it derives from java.lang.Object?

If anybody could add more detail, please do...

Cheers,
Raj.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Raj Kamal wrote:
If a Class does not extend from any other then the compiler creates the Object hierarchy such that it derives from java.lang.Object?


If the definition of a class doesn't include "extends X", that's the same as including "extends java.lang.Object". I can't really think of anything to add to that.
Rajkamal Pillai
Ranch Hand

Joined: Mar 02, 2005
Posts: 443
    
    1


I am not able to think of any reasons other than providing the functionality of Object class. I mean there are quite a few functionality in java.lang.Object. If the user defined class(es) are made to be inherited from it all those will also be inherited. Is there any reason other than that?

Cheers,
Raj.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

It's just how Java is defined to work: all classes have class java.lang.Object at the top of their inheritance hierarchy. Because of that, the methods defined in class java.lang.Object are available on all classes. The people who designed the Java language thought that it would be a good idea to have a common superclass for all classes, where they could put special functionality in. There's not much more to it.

One example of functionality that's available on all objects is the following: all Java objects have a lock, so that you can synchronize on any object. Class Object contains some methods to support this (wait, notify and notifyAll).

Also, all objects have an equals, hashCode and toString method, because those are defined in class Object.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Rajkamal Pillai
Ranch Hand

Joined: Mar 02, 2005
Posts: 443
    
    1

Thank you Ernest and Jesper.

I was trying to understand if there were more reasons than the obvious.

Cheers,
Raj.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38412
    
  23
CampbellsComputer$:javac Foo.java
CampbellsComputer$:javap Foo
CampbellsComputer$:javap -c Foo
... and see what prints out on screen. Look at the top line of the printout of the .class file, with and without the -c
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: All classes extend java.lang.Object. But how does this work?