Object Class
Purpose:
- The standard class
Objectis a superclass of all other classes. It is in thejava.langpackage.
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
Objectcan 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
Objectare as follows:- The method
getClassreturns theClassobject that represents the class of the object. AClassobject 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
toStringreturns a String representation of the object. - The methods
equalsandhashCodeare declared for the benefit of hashtables such asjava.util.Hashtable. The methodequalsdefines a notion of object equality, which is bosed on value, not reference, comparison. - The method
cloneis used to make a duplicate of an object. - The methods
wait,notifyandnotifyAllare used in concurrent programming using threads. - The method
finalizeis run just before an object is destroyed.
- The method
Restrictions:
- You do not need to extend
java.lang.Object. It is done for you at compile time.
email suggestions to: cs015tas@cs.brown.edu
