Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

object

jvm

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?

like image 608
Imago Avatar asked Mar 01 '16 18:03

Imago


People also ask

Can we create a class without an object?

Static method can be called without creating an object or instance. Simply create the method and call it directly. This is in a sense orthogonal to object orientated programming: we call a method without creating objects.

Can object class be a base class?

The Object class is the base class for all the classes in the . Net Framework.

Which class is used to design to work as a base class only no objects are created for that class?

Here, Base is an abstract class (because it has a pure virtual function), so no objects of class Base can be directly created: Base is (explicitly) meant to be a base class.

Is the object class the base class of all classes?

Object class is the root or superclass of the class hierarchy, which is present in java. lang package. All predefined classes and user-defined classes are the subclasses from Object class.


2 Answers

The Object class itself is indeed special. If you take a look at it's implementation in rt.jar in your jre folder, you will notice most of it's methods are just declarations with a native modifier.

package java.lang;

public class Object
{
  private static native void registerNatives();

  public final native Class<?> getClass();

  public native int hashCode();

  public boolean equals(Object paramObject)
  {
    return this == paramObject;
  }

  protected native Object clone()
    throws CloneNotSupportedException;

  public String toString()
  {
    return getClass().getName() + "@" + Integer.toHexString(hashCode());
  }

  public final native void notify();

  public final native void notifyAll();

  public final native void wait(long paramLong)
    throws InterruptedException;

  public final void wait(long paramLong, int paramInt)
    throws InterruptedException
  {
    if (paramLong < 0L) {
      throw new IllegalArgumentException("timeout value is negative");
    }
    if ((paramInt < 0) || (paramInt > 999999)) {
      throw new IllegalArgumentException("nanosecond timeout value out of range");
    }
    if (paramInt > 0) {
      paramLong += 1L;
    }
    wait(paramLong);
  }

  public final void wait()
    throws InterruptedException
  {
    wait(0L);
  }

  protected void finalize()
    throws Throwable
  {}

  static {}
}

Those native methods are implemented inside the JVM itself. These methods are the bridge between the native and managed parts of a Java program. Because of this, it is natural to have this Object class as "the common things in every object". The Java language is specified in a way that there is no way to build a separate class hierarchy besides Object.

About modifying Object

Although I haven't tried it yet, it might be possible to modify the original Object class. It's sure that the existing methods cannot be removed, because most of the runtime relies on them, but I see a chance you can add new methods by modifying the runtime itself.

It seems you cannot use java agents to modify the Object, String, ClassLoader, Class<?>, ProtectionDomain, IllegalClassFormatException, and array classes.

like image 186
Tamas Hegedus Avatar answered Oct 02 '22 18:10

Tamas Hegedus


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.

like image 44
wero Avatar answered Oct 02 '22 16:10

wero