Understanding Object class


Object class is a super class of all class in Java. All inheritance hierarchies starts from Object class followed by other class. Consider example class Shape as parent class of Rectangle and Circle class, in that case Object class is parent or super class of Shape class hence Rectangle and Circle class not only get methods and properties of Shape class but also Object class. This article shows the methods of Object class which is inherited by all the classes that are predefined or user defined.

 Methods of Object class

  • protected Object clone()
  • boolean equals(Object obj)
  • protected void finalize()
  • Class<?> getClass()
  • int hashcode()
  • String toString()
  • void notify()
  • void notifyAll()
  • void wait()
  • void wait(long timeout)
  • void wait(long timeout, int nanos)

Notify and wait methods are related to Threads.

  • String toString()
    • Returns the string representation of object.
  • protected void finalize()
    • This method is called by garbage collector before destroying the object.
  • protected Object clone()
    • Used to create a new object from copying existing object.
  • boolean equals(Object obj)
    • Used to check the equality of two objects.
  • int hashCode()
    • Returns the hash code of the object.

toString method

class Employee
{
	private int eid;
	private String name;
	private String dept;
	private int salary;
	public Employee(int eid, String name, String dept, int salary)
	{
		this.eid = eid;
		this.name = name;
		this.dept = dept;
		this.salary = salary;
	}

}
class Test
{
	public static void main(String args[])
	{
		Employee emp = new Employee(1, "ABC", "Purchase" , 30000);
		System.out.println(emp);
	}
}

D:\>javac Test.java

D:\>java Test
Employee@15db9742

 

When we try to print the object emp using System.out.println it prints classname@somehexvalue. Internally Java calles the toString method which is inherited by Employee class from Object class. Following code overrides the toString method to return the String representation of the object.

class Employee
{
	private int eid;
	private String name;
	private String dept;
	private int salary;
	public Employee(int eid, String name, String dept, int salary)
	{
		this.eid = eid;
		this.name = name;
		this.dept = dept;
		this.salary = salary;
	}
	public String toString()
	{
		return "id : " + eid + " Name : " + name + 
			" Department : " + dept + " Salary : " + salary;
	}

}
class Test
{
	public static void main(String args[])
	{
		Employee emp = new Employee(1, "ABC", "Purchase" , 30000);
		System.out.println(emp);
	}
}

D:\>javac Test.java

D:\>java Test
id : 1 Name : ABC Department : Purchase Salary : 30000

equals method

It is used to check the equality between the two objects. Lets understand with the example. Consider Employee class, Create two instance with same value and compare the same, shown in example below

class Employee
{
	private int eid;
	private String name;
	private String dept;
	private int salary;
	public Employee(int eid, String name, String dept, int salary)
	{
		this.eid = eid;
		this.name = name;
		this.dept = dept;
		this.salary = salary;
	}
	public String toString()
	{
		return "id : " + eid + " Name : " + name + 
			" Department : " + dept + " Salary : " + salary;
	}

}
class Test
{
	public static void main(String args[])
	{
		Employee emp1 = new Employee(1, "ABC", "Purchase" , 30000);
		System.out.println(emp1);
		Employee emp2 = new Employee(1, "ABC", "Purchase" , 30000);
		System.out.println(emp2);
		System.out.println(emp1==emp2);
		Employee emp3 = emp1;
		System.out.println(emp1==emp3);
	}
}

Output
id : 1 Name : ABC Department : Purchase Salary : 30000
id : 1 Name : ABC Department : Purchase Salary : 30000
false
true

We can see that when we compare two Employee object with same value using == operator it gives false and when we compare two Employee object with same reference it returns true. Above case can be understand by following diagram

emp_object_java_diagram

As Employee is custom class or user defined class, we need to provide the logical implementation as how to decide equality of two objects. By default every class inherits equals method from Object class whose default implementation is to compare the reference rather than the actual value. We can override the equals method as shown below

class Employee
{
	private int eid;
	private String name;
	private String dept;
	private int salary;
	public Employee(int eid, String name, String dept, int salary)
	{
		this.eid = eid;
		this.name = name;
		this.dept = dept;
		this.salary = salary;
	}
	public String toString()
	{
		return "id : " + eid + " Name : " + name + 
			" Department : " + dept + " Salary : " + salary;
	}
	public boolean equals(Object obj)
	{
	    Employee e = (Employee)obj;
	    if(this.eid==e.eid & this.name.equals(e.name) & this.dept.equals(e.dept) & this.salary==e.salary)
	    {
	        return true;
	    }
	    else
	        return false;
	}

}
class Test
{
	public static void main(String args[])
	{
		Employee emp1 = new Employee(1, "ABC", "Purchase" , 30000);
		System.out.println(emp1);
		Employee emp2 = new Employee(1, "ABC", "Purchase" , 30000);
		System.out.println(emp2);
		System.out.println("emp1==emp2 : " + (emp1==emp2));
		System.out.println("emp1.equals(emp2) : " + emp1.equals(emp2));		
		Employee emp3 = emp1;
		System.out.println(emp1==emp3);
	}
}
Output
id : 1 Name : ABC Department : Purchase Salary : 30000
id : 1 Name : ABC Department : Purchase Salary : 30000
emp1==emp2 : false
emp1.equals(emp2) : true
true

By overriding the equals method we can now compare two objects on the basis of the values. == operator works well for the primitive data type, for reference data type == will compare the reference and equals if overridden will compare the actual value.

Leave a comment

Your email address will not be published. Required fields are marked *