Object Class in Java

Object class

In the previous blog we have learned about the Abstract Class in Java, abstract methods, etc. Please visit the blog by clicking Abstract Class in Java. In this blog, we will go through the Object Class - Cosmic Superclass. Object class is the ancestor for every class. Every class in Java extends the Object class. We don’t have to specifically write the following:

public AtrowelEmployee extends Object

If there is no superclass mentioned explicitly, then Object is considered a superclass. As every class in Java extends the Object class, it's important to be familiar with the services provided by it. We will go through it:

Object Type Variable in Java

We can use a variable of type Object to refer to the object of any type. For example,

Object obj = new AtrowelEmployee("John Smith",50000);

Type Object is only used as a generic holder for arbitrary values. To do things with the values, we must have knowledge about the original data type and apply a cast. For example

AtrowelEmployee atroweObj  = (AtrowelEmployee) obj;

The value of primitive data types i.e. int, boolean, char. etc are not objects in Java. All the array types, like array of objects or arrays of primitive types, are class types that extend the Object class. For example

AtrowelEmployee[] atrowelStaff = new Employee[20];
  obj = atrowelStaff;
  obj = new int[20];

equals() method

equals() method is used for checking the two objects. i.e. it tests whether one object is equal to another object. equals() method in the Object class is used for checking if the two objects are identical or not. If the two objects are identical, then they will be equal. For example, let's take the example of two employees having the same name, salary, and hiring date.

e.g.

public class AtrowelEmployee{
  . . .
  public boolean equals(Object atrowelObject){
    if (this == atrowelObject)
      return true;
    if (atrowelObject == null)
      return false;
    if (getClass() != atrowelObject.getClass())
      return false;
    Employee atrowelObject1 = (Employee) atrowelObject;
    return employeeName.equals(atrowelObject1.employeeName) && employeeSalary == atrowelObject1.employeeSalary && employeeHireDay.equals(atrowelObject1.employeeHireDay);
  }
}

In the above example the getClass() method returns the class of an object. The two objects can only be equal when they belong to the same class. When we want to define the equals() method for the child class(subclass), then we have defined it first in the parent class(superclass). If both the objects(object of superclass and subclass)are not equal then it will return false. If the fields of the superclass are equal, then we can compare the fields of the subclass.

e.g.

public class AtrowelManager extends AtrowelEmployee{
  . . .
  public boolean equals(Object atrowelObject){
    if (!super.equals(atrowelObject))
      return false;
    AtrowelManager atrowelObject1 = (Manager) atrowelObject;
    return bonus == atrowelObject1.bonus;
  }
}

Equality Testing and Inheritance in Java

You may be having question about how the equals() method behaves when implicit and explicit parameters don’t belong to the same class?. It is an area of controversy. In the above example, we have seen that the equals() method returns false if the classes don’t match. Many developers use instanceof instead:

if (!(atrowelObject instanceof AtrowelEmployee))
  return false;

This approach may create problems. Now you may be thinking how, so let me explain. equals() method has the following properties:

  1. Reflexive - If there is a non-null reference i.e. obj, then obj.equals(obj) should return true.
  2. Symmetric - If we have two references i.e. atrowelObj and atrowelObj2, then atrowelObj.equals(atrowelObj1) should return true if and only if atrowelObj1.equals(atrowelObj) returns true.
  3. Transitive - If we have references like atrowelObj, atrowelObj1, and atrowelObj2, and if atrowelObj.equals(atrowelObj1) returns true and atrowelObj1.equals(atrowelObj2) returns true, then atrowelObj.equals(atrowelObj2) should return true.
  4. Consistent - If the objects to which atrowelObj and atrowelObj1 refers haven’t changed, then repeated calls to atrowelObj.equals(atrowelObj1) returns the same value.
  5. If there is a non-null reference atrowelObj, then atrowelObj.equals(null) should return false.

The symmetry rule has consequences when the parameters belong to different classes. For example consider a call:

atrowelObj.equals(atrowelObj1);

where atrowelObj is an AtrowelEmployee object and atrowelObj1 is an AtrowelManager object, having the same name, salary, and hire date. If AtrowelEmployee.equals uses an instanceof, the call returns true, which means the reverse call:

atrowelObj1.equals(atrowelObj);

The above statement also needs to return true, But the symmetry rule does not allow it to return false or to throw an exception. It leaves the AtrowelManager class in a bind. Its equals() method must be willing to compare itself to any Employee, without taking manager-specific information into account. All of a sudden, the instanceof looks less attractive. We can use getClass and instanceof when we have the following scenarios:

  1. If we consider the example AtrowelManager and AtrowelEmployee class, the two objects will be equal only if they have matching fields. If we have two AtrowelManager objects with the same name, salary, and hire date, but with different bonuses, and we want them to be different, then we use the getClass test.
  2. But suppose we used an empId(employee ID) for equality testing. This will test the equality for all subclasses. Then in this case we could use the instanceof test, and we should have declared Employee.equals as final.

Object toString()

toString() is one of the important methods in Object. toString() method returns the string representing the value of this object. Most toString methods follow the format: the name of the class, then the field values enclosed in square brackets([ ]). We will see it with the help of an example

e.g.

public String toString(){
  return "AtrowelEmployee[name=" + atrowelName + ",salary=" + atrowelSalary + ",hireDay=" + atrowelHireDay+ "]";
}

In the above example we can see the implementation of the toString() method. toString() method returns the fields of AtrowelEmployee class. We can make the above code more precise. Instead of writing the AtrowelEmployee class in the toString() method we can call getClass().getName() to obtain a string with the class name.

e.g.

public String toString(){
  return getClass().getName() + "[name=" + name + ",salary=" + salary + ",hireDay=" + hireDay + "]";
}

The above toString() method implementation can work for all subclasses. If the superclass uses getClass().getName(), then the subclass can simply call super.toString().

e.g.

public class AtrowelManager extends AtrowelEmployee{
  . . .
  public String toString(){
    return super.toString() + "[bonus=" + atrowelBonus + "]";
  }
}

The toString method is used for an important reason i.e. Whenever an object is concatenated with a string by the “+” operator, then the Java compiler automatically invokes the toString() method to obtain a string representation of the object.

e.g.

var object = new AtrowelEmployee("Jackson",50000);
String message = "The value of the object" + object; // it automatically invokes object.toString()

Suppose we have an object i.e. atrowelObj and we call

e.g.

System.out.println(atrowelObj);

In the above statement println() simply calls atrowelObj.toString() and prints the string.In the following ways we can use the toString() to get the value of the object.

Get in Touch

Atrowel will be pleased to receive your feedback and suggestions. Those of you who would like to contribute, please send us an email.