Object Class

Purpose:

The standard class Object is a superclass of all other classes. It is in the java.lang package.

Mechanics:


package java.lang;

public class Object {
     public final Class getClass() { ... }
     public String toString() { ... }
     public boolean equals(Object obj) {...}
     public int hashCode() { ... }
     protected Object clone()
        throws CloneNotSupportedException { ... }
     public final void wait()
        throws IllegalMonitorStateException,
           InterruptedException { ... }
     public final void wait(long millis)
        throws IllegalMonitorStateException,
           InterruptedException { ... }
     public final void wait(long millis, int nanos) { ... }
        throws IllegalMonitorStateException,
           InterruptedException { ... }
     public final void notify() { ... }
        throws IllegalMonitorStateException
     public final void notifyAll() { ... }
        throws IllegalMonitorStateException
     protected void finalize()
        throws Throwable { ... }
}

Usage:

  • A variable of type Object can hold a reference to any object, whether it is an instnace of a class or an array.
  • All class and array types inherit the methods of class Object.
  • The methods of Object are as follows:
    • The method getClass returns the Class object that represents the class of the object. A Class object exists for each reference type. It can be used, for example, to discover the fully qualified name of a class, its members, its immediate superclass, and any interfaces that it implements.
    • The method toString returns a String representation of the object.
    • The methods equals and hashCode are declared for the benefit of hashtables such as java.util.Hashtable. The method equals defines a notion of object equality, which is bosed on value, not reference, comparison.
    • The method clone is used to make a duplicate of an object.
    • The methods wait, notify and notifyAll are used in concurrent programming using threads.
    • The method finalize is run just before an object is destroyed.

Restrictions:

You do not need to extend java.lang.Object. It is done for you at compile time.
Back to the Table of Contents
email suggestions to: cs015tas@cs.brown.edu