13

Why is object class the super class in Java?

2
  • 5
    Not really. See C++ for a prominent exception.
    – user207421
    Mar 22, 2010 at 9:21
  • 1
    Well neither does Java. You could view Java as being multirooted with the primitives joining Object as the roots. (Or you might think that is a bit of a hack around the naive design of Object.) Mar 22, 2010 at 14:59

6 Answers 6

16

That is a good question. Java chose to make a single class be the ultimate parent class for everything so that there is an easy way to pass around any arbitrary object, without needing to know its type (i.e. you can use the declared type of Object to refer to every single item in the type system, even primitives using their wrapper classes). However, there are OOP languages such as C++ where there is no universal base class as in Java. Another benefit to having a universal base class is that logic dealing with the superclass does not have to be special cased for top-level classes (with the exception of the universal base class, Object, itself).

2
  • 1
    Could you please explain the second benefit?
    – abson
    Mar 22, 2010 at 9:21
  • 2
    @abson, basically the "super" keyword doesn't need to be implemented in two different ways, since "super" will always refer to some base type. This is only a minor benefit. Mar 22, 2010 at 9:24
4

It's what we call the axiom of object-oriented programming in Java. Every single abstraction in your code is an object. It contains a few things that are applicable to every peace of information you use in your code:

  • equals and hashCode methods to establish an equality theory within the given abstraction (see corresponding javadoc);
  • toString to represent an object in human-readable (probably, only programmer-readable) format (because most of us still use displays and keyboards);
  • getClass to provide reflection capabilities on the given abstraction;
  • some methods to organize object-oriented runtime (garbage collection, synchronization, etc.).

If you learn Java, it is best for you to study the "Inheritance" section of whatever book you use and then try to answer this question yourself.

0
1

Off the top of my head

  1. Defining 'Object' as the root class ensures the VM can rely upon the interface provided for utility methods such as equals, clone, hashcode etc

  2. The Garbage collector can ensure anything that the user deemed reconcilable can be executed in the finalize method

1

It provides a template for all the derived objects that programmers create. The key functionalities that may be required for every user-defined object are readily available this way.

  • Object locking (For concurrency-problem resolution)

  • Cloning

  • Hashcode generation

To name the more important ones.

1

Java classes (implicitly/explicitly) extends Object for the following reasons:

  • Java needs to run on every platform that exists (be it mobile, Windows, Linux, etc.) and writing a vast amount of code to run on each platform was to be a mission. Hence, Java created native. That way, all objects gets registered as a native object that the JVM can understand and execute. Don't forget that JVM is written in C++ (irrespective of the platform it's executed on).
  • For Garbage Collection, the finalize method is called by the JVM when the Object is not needed.

The rest have been mentioned here already.

4
  • -1: "all objects get registered as a native object"? That only happens when intentionally using JNI... unless you meant something else? Also, the finalize method isn't guaranteed to be called... though that's a bit nitpicky. Also, there's no need for JVMs to be written in C/C++ - in fact, there exists a pretty cool one written in java that runs itself.
    – amara
    May 16, 2011 at 6:57
  • @Vuntic, if the JVM is fully written in Java, how does it handle file systems, file descriptor tables, OS system properties, or anything OS specific? Java wasn't designed to know what OS it is running, but to run independently. The JVM has to do that and, unless it knows how to talk with the machine assembly in java, the only way to translate bytecodes to run on OS is to run it through a HAL, which I never seen written in Java (unless I am wrong). May 16, 2011 at 9:16
  • It's possible: stackoverflow.com/questions/2279229/… . Apologies if the downvote was unwarranted, but I found this answer inaccurate and confusing.
    – amara
    May 16, 2011 at 11:18
  • @sparkleshy, what is inaccurate and confusing? Sep 20, 2013 at 10:56
0

The java program is essentially manipulation of

method and class.

The class inherit some properties on which there structure built up later.

Object is the basic class and all classes are subclass of Object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.