Being a Descendent of Object

You should read the textbook p209-213 or Being a Descendent of Object
The Object class sits at the top of the class hierarchy tree in the Java platform. Every class in the Java system is a descendent, direct or indirect, of the Object class. This class defines the basic state and behavior that all objects must have, such as the ability to compare oneself to another object, to convert to a string, to wait on a condition variable, to notify other objects that a condition variable has changed, and to return the class of the object.

Your classes may want to override the following Object methods. The equals/hashCode are listed together as they must be overridden together.

Your class cannot override these Object methods (they are final):

Examples : Coord.java

We start with a class call Coord that represent a coordinate. Here is the class
public class Coord {
   public int x;
   public int y;
   Coord(int x, int y) {
     this.x = x;
     this.y = y;
   }
}

The toString Method

Object's toString method returns a String representation of the object. You can use toString along with System.out.println to display a text representation of an object, such as
System.out.println(new Dimension(5,7).toString);
or we can even use
System.out.println(new Dimension(5,7));
The String representation for an object depends entirely on the object. The String representation of an Integer object is the integer value displayed as text. The String representation of a Dimension object contains various members such as width and height. For example, the previous lines of code displays the following output:
java.awt.Dimension[width=5, height=7]
The toString method is very useful for debugging. It behooves you to override this method in all your classes.

Question

What is the output of
System.out.println(new Coord(1,2));

Answer

something strange like : Coord@720eeb
So we should rewrite Coord like this
public class Coord {
   public int x;
   public int y;
   Coord(int x, int y) {
     this.x = x;
     this.y = y;
   }
   
   public String toString() {
      return "("+ x + "," + y + ")";
   }
}

The equals and hashCode Methods

You should override the equals and hashCode methods together. (We won't talk about hashCode, it is used in hash table).

The equals method compares two objects for equality and returns true if they are equal. The equals method provided in the Object class uses the identity function to determine if objects are equal (if the objects compared are the exact same object the method returns true).

However, for some classes, two distinct objects of that type might be considered equal if they contain the same information.

Example

Coord c1 = new Coord(5,6);
Coord c2 = new Coord(5,6);
System.out.println(c1.equals(c2));
gives false.
The new code should be
public class Coord {
    public int x;
    public int y;
    public Coord(int x, int y) {
	this.x = x;
	this.y = y;
    }
    
    public boolean equals(Coord c) {
   	return x == c.x && y == c.y;
   }
    
   public String toString() {
      return "("+ x + "," + y + ")";
   }
}
A better program is
public class Coord {
    public int x;
    public int y;
    public Coord(int x, int y) {
	this.x = x;
	this.y = y;
    }
    
    public boolean equals(Object obj) {
        if(obj instanceof Coord) { // if obj is of type Coord
            Coord c = (Coord)obj; // type casting
   	    return x == c.x && y == c.y;
        } else { // obj is of different type
            return false;
        }
   }
    
   public String toString() {
      return "("+ x + "," + y + ")";
   }
}

The finalize Method

The Object class provides a method, finalize, that cleans up an object before it is garbage collected. The finalize method is called automatically by the system and most classes you write do not need to override it. So you can generally ignore this method.

The getClass Method

The getClass method is a final method that returns a runtime representation of the class of an object. This method returns a Class object.

Once you have a Class object you can query it for various information about the class, such as its name, its superclass, and the names of the interfaces that it implements. The following method gets and displays the class name of an object:

void PrintClassName(Object obj) {
    System.out.println("The Object's class is " +
                       obj.getClass().getName());
}