• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Rajkamal Pillai
Ranch Hand
Posts: 445
1
Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 445
1
Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rajkamal Pillai
Ranch Hand
Posts: 445
1
Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Ernest and Jesper.

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

Cheers,
Raj.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47232
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic