Can you create a class structure that does not have Object as the base class

advertisements

As far as a I know: Every class in Java inherits the methods of the Object Class without the need of specifying to do so, which makes this class so unique and interesting.

Thus I wonder, where is this "rule" specified, inside the JVM? Can this class be somehow manipulated, for example adding or removing a method or variable? Is it possible to create a parallel, hierarchical structure independently of the Object class?


If you want to create a class which does not derive directly or indirectly from java.lang.Object you must create a class file which does not have a superclass specified.

Now if you look at the JDK code which parses a class file you will find that the class file parser does this check:

if (super_class_index == 0) {
  check_property(class_name == vmSymbols::java_lang_Object(),
                 "Invalid superclass index %u in class file %s",
                 super_class_index,
                 CHECK_(nullHandle));
} else {

so it would reject your base class. Only java.lang.Object can have no superclass.