Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
200 views
in Technique[技术] by (71.8m points)

Why does every object in Java implicitly extend java.lang.Object class?

I have been programming in Java for quite some time, but when I tried to explain what an java.lang.Object class is to a friend, I could not come up with more than a simple one-liner:

All objects in Java extend java.lang.Object implicitly

I was not quite sure why it should do so.

So, I looked upon the source code on GrepCode, hoping that I can find some clues. Now I know what a java.lang.Object is and what it does, I want to know if there was any specific reason as to why it was designed this way.

My question still prevails: why should every object extend java.lang.Object?

question from:https://stackoverflow.com/questions/19114997/why-does-every-object-in-java-implicitly-extend-java-lang-object-class

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I would say that the reason is to have a common API for all objects in java to supports basic functionality like

  • synchronization - wait, notify, notifyAll
  • garbage collection - finalize
  • collection support - hashCode, equals
  • object cloning - clone

And every object

  • has a class it belongs to - getClass
  • can represent itself as a string, because we are humans and can read strings - toString

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...