Methods Inherited from Object

Contact Us or call 1-877-932-8228
Methods Inherited from Object

Methods Inherited from Object

There are a number of useful methods defined for Object.

Some are useful as is, such as:

Class getClass() - returns a Class object (a representation of the class that can be used for comparisons or for retrieving information about the class).

Others are useful when overridden with code specific to the new class:

Object clone() - creates a new object that is a copy of the original object. This method must be overridden, otherwise an exception will occur (the Object version of clone throws a CloneNotSupportedException).

The issue is whether to perform a shallow copy or a deep copy - a shallow copy merely copies the same reference addresses, so that both the original object and the new object point to the same internal objects; a deep copy makes copies of all the internal objects (and then what if the internal objects contained references to objects ...? ).

boolean equals(Object) - does a comparison between this object and another. If you don't override this method, you get the same result as if you used == (that is, the two references must point to the same object to compare as equal - two different objects with the same field values would compare as unequal) - that is how the method is written in the Object class. You would override this method with whatever you need to perform a comparison.

int hashCode() - returns an integer value used by collection objects that store elements using a hashtable. Elements that compare as the same using the equals(Object) method should have the same hashcode.

String toString() - converts this object to a string representation.

This method is called by a some elements in the Java API when the object is used in a situation that requires a String, for example, when you concatenate the object with an existing String, or send the object to System.out.println().

Note that the call to toString is not made automatically as a typecast to a String - the only behavior built into the syntax of Java is string concatenation with the + sign (so one of the operands must already be a String); the code in println is explicitly written to call toString.

If you don't override this method, you will get a strange string including the full class name and the hashcode for the object void finalize() - called by the JVM when the object is garbage-collected. This method might never be called (the program may end without the object being collected).

There are also a several methods (wait, notify, and notifyAll) related to locking and unlocking an object in multithreaded situations.

Code Sample:

public class ObjectMethods {
  int id;
  String name;
  int age;

  ObjectMethods(int id, String name, int age) { = id; = name;
    this.age = age;
  public boolean equals(Object x) {
    if (x == this) return true;
    else if (x instanceof ObjectMethods) {
      ObjectMethods omx = (ObjectMethods) x;
      return id == && name.equals( && age == omx.age;
    else return false;
  public int hashCode() {
    return id + age * 1000;
  public String toString() {
    return id + " " + name + " is " + age + " years old";
  public Object clone() {
    return new ObjectMethods(id, name, age);
  public static void main(String[] args) {
    ObjectMethods om1 = new ObjectMethods (1, "John", 6);
    ObjectMethods om2 = new ObjectMethods (1, "John", 6);
    ObjectMethods om3 = new ObjectMethods (2, "Jane", 5);
    ObjectMethods om4 = (ObjectMethods)om3.clone();
    System.out.println("Printing an object: " + om1);
    if (om1.equals(om2)) 
      System.out.println("om1 equals(om2)");
    if (om1.equals(om3))
      System.out.println("om1 equals(om3)");
    if (om1.equals("John"))
      System.out.println("om1 equals(\"John\")");
    if (om3.equals(om4))
      System.out.println("om3 equals(om4) which was cloned from om3");
    System.out.println("object class is: " + om1.getClass());

The clone method returns Object rather than ObjectMethods, since that is how it was declared in the Object class, and you can't change the return type when overriding - thus the typecast on the returned value.

Similarly, the parameter to equals is Object, rather than ObjectMethods. This is not required by Java's syntax rules, but rather a convention that enables other classes to work with this class. For example, the Collections API classes use the equals method to determine if an object is already in a set. If we wrote the method as equals(ObjectMethods om) instead of equals(Object o), the collections classes would call equals(Object o) as inherited from Object, which would test for identity using an == test.

The hashCode method was written out of a sense of duty - Sun specifies that the behavior of hashCode "should be consistent with equals", meaning that if two items compare as equal, then they should have the same hash code - this will be revisited in the section on Collections.