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?
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.
The Object class is the base class for all the classes in the . Net Framework.
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.
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.
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
.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With