Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 520
codeMan Avatar asked Oct 01 '13 11:10

codeMan


People also ask

Does every Java class extend Object?

Yes, since Object is the parent of all classes, then every class must extend Object (directly xor indirectly).

What class does every class implicitly extend?

Excepting Object , which has no superclass, every class has one and only one direct superclass (single inheritance). In the absence of any other explicit superclass, every class is implicitly a subclass of Object .

What class does every class in Java extend inherit from implicitly?

Every class has Object as a superclass. All objects, including arrays, implement the methods of this class.

Why do we extend classes in Java?

The extends keyword extends a class (indicates that a class is inherited from another class). In Java, it is possible to inherit attributes and methods from one class to another. We group the "inheritance concept" into two categories: subclass (child) - the class that inherits from another class.


5 Answers

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
like image 78
René Link Avatar answered Oct 19 '22 12:10

René Link


I think the most important use of Object is not to provide common methods like toString() but to provide a common type that would hold all reference types.

C++ don't have an Object equivalent and people are still happy. But since Java don't have pointers and C++-like templates, Object is required to make implementations of Collections, etc. possible.

See also on discussions on reference and primitive types.

like image 9
billc.cn Avatar answered Oct 19 '22 12:10

billc.cn


This is how the language is designed. Every object will inherit from the base class Object. This means that it's guaranteed for every object there will be certain methods, like toString(), equals(), hashCode(), etc.

like image 9
Konstantin Yovkov Avatar answered Oct 19 '22 12:10

Konstantin Yovkov


I would say Design. Common/Mandatory methods which every Object should support written there and extending that class as a language specification.

You find the reasons here in Official Docs.

If we are saying this is an Object ,They must have the common methods, Which defined/decided by API.

Imagine the below methods for every class on your Own.

protected Object clone() throws CloneNotSupportedException
      Creates and returns a copy of this object.


public boolean equals(Object obj)
      Indicates whether some other object is "equal to" this one.


protected void finalize() throws Throwable
      Called by the garbage collector on an object when garbage
      collection determines that there are no more references to the object


public final Class getClass()
      Returns the runtime class of an object.


public int hashCode()
      Returns a hash code value for the object.


public String toString()
      Returns a string representation of the object.

The notify, notifyAll, and wait methods of Object all play a part in synchronizing the activities of independently running threads in a program:

public final void notify()
public final void notifyAll()
public final void wait()
public final void wait(long timeout)
public final void wait(long timeout, int nanos) 

So to reduce the pain, created a common and standard API.

like image 5
Suresh Atta Avatar answered Oct 19 '22 12:10

Suresh Atta


Every Class extends Object class implicitly so that they provide basic features which according to Java recommendation every class should have. Such as clone(), equals(), hashCode(), toString(), etc.

By implicitly, it means that if you are not extending any class then only compiler will implicitly extends Object class.But if class already extends other class then compiler will not extend Object class. For eg. Class A{ }

Class B extends A{ }

Here compiler will implicitly add extends Object class in class A declaration.

Class A extends Object{ }

Class B extends A{ }

As class A extends Object class so it will provide basic functionality of Object class such as equals(), toString(),etc. And since Class B extends class A which implicitly extends Class Object, so class B also provides all those features.

Thus by following this approach every class objects(variables) complies to features which every Java Object should have, without going for Multiple Inheritance (a class extending more than one class) which Java doesn't allows. This approach follows Multi-Level Inheritance.

like image 5
user1425349 Avatar answered Oct 19 '22 12:10

user1425349