1. The most common general methods which can be applicable on any java object are defined in object class.
  2. Object class is the parent class of any java class, whether it is predefined or programmer defined, hence all the object class methods are by default available to any java class.

Object class define the following 11 methods.

Constructor Description
clone() Creates a new object of the same class as this object.
equals(Object) Compares two Objects for equality
finalize() Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.
getClass() Returns the runtime class of an object.
hashCode() Returns a hash code value for the object.
notify() Wakes up a single thread that is waiting on this object’s monitor.
notifyAll() Wakes up all threads that are waiting on this object’s monitor.
toString() Returns a string representation of the object.
wait() Waits to be notified by another thread of a change in this object.
wait(long) Waits to be notified by another thread of a change in this object.
wait(long, int) Waits to be notified by another thread of a change in this object.

toString() method implementation and usage:

It is used to return string representation of an object.
Example:

public class Customer {

	private int customerID;
	private String customerName;

	public Customer(int customerID, String customerName) {

		this.customerID = customerID;
		this.customerName = customerName;
	}

	public static void main(String[] args) {

		Customer customer1 = new Customer(999, "BMW");
		Customer customer2 = new Customer(888, "FORD");
		System.out.println("customer1::" + customer1);
		System.out.println("customer2::" + customer2);
	}
}
/*
Output:
customer1::com.java2learn.javalang.Customer@3e25a5
customer2::com.java2learn.javalang.Customer@19821f
*/
  1. When ever we are passing object reference as argument to System.out.println() method internally JVM will call toString() on that object.
  2. If we are not providing toString() then Object class toString() will be executed which is implemented as follows
  3. public String toString(){
       Return getClass.getName() + ‘@’ + Integer.toHexString(hashCode);
    }
    
  4. Based on our requirement to provide our own String representation we have to override toString().
  5. If we want print Customer object reference with customerID and name,we have to override the toString() method as follows.
 public String toString() {
        return "Customer [customerID=" + customerID + ", customerName="
                + customerName + "]";
    }

Example:


public class Customer {

	private int customerID;
	private String customerName;

	public Customer(int customerID, String customerName) {

		this.customerID = customerID;
		this.customerName = customerName;
	}

	@Override
	public String toString() {
		return "Customer [customerID="+customerID+",customerName="
				+ customerName + "]";
	}

	public static void main(String[] args) {

		Customer customer1 = new Customer(999, "BMW");
		Customer customer2 = new Customer(888, "FORD");
		System.out.println(customer1);
		System.out.println(customer2);
	}
}
/*
Output:
Customer [customerID=999, customerName=BMW]
Customer [customerID=888, customerName=FORD]
*/

hashCode() method implementation and usage:

  1. The hashCode of an Object just represents a random number which can be used by JVM while saving/adding Objects into Hashsets, Hashtables or Hashmap.
  2. hashCode() of Object class implemented to return hashCode based on address of an object, but based on our requirement we can override hashCode() to generate our own numbers as hashCodes.

Example:

public class HashCodeDemo {

	private int count;
	
	public HashCodeDemo(int count) {
		
		this.count = count;
	}

	@Override
	public int hashCode() {
		return count;
	}

	public static void main(String[] args) {
		
		HashCodeDemo hashCodeDemo1=new HashCodeDemo(100);
  //com.java2learn.javalang.HashCodeDemo + ‘@’ + Integer.toHexString(100)
		System.out.println(hashCodeDemo1);
		HashCodeDemo hashCodeDemo2=new HashCodeDemo(120);
  //com.java2learn.javalang.HashCodeDemo + ‘@’ + Integer.toHexString(120)
		System.out.println(hashCodeDemo2);
	}
}
/*
Output:
com.java2learn.javalang.HashCodeDemo@64
com.java2learn.javalang.HashCodeDemo@78
*/

Example:

public class HashCodeDemo {

	private int count;
	
	public HashCodeDemo(int count) {
		
		this.count = count;
	}

	@Override
	public int hashCode() {
		return count;
	}

	
	@Override
	public String toString() {
		return "HashCodeDemo [count=" + count + "]";
	}

	public static void main(String[] args) {
		
		HashCodeDemo hashCodeDemo1=new HashCodeDemo(100);
		System.out.println(hashCodeDemo1);
		HashCodeDemo hashCodeDemo2=new HashCodeDemo(120);
		System.out.println(hashCodeDemo2);
	}
}
/*
Output:
HashCodeDemo [count=100]
HashCodeDemo [count=120]
*/

equals() method implementation and usage:
Example:

package com.java2learn.javalang;

public class Customer {

	private int customerID;
	private String customerName;

	public Customer(int customerID, String customerName) {

		this.customerID = customerID;
		this.customerName = customerName;
	}

	public static void main(String[] args) {

		Customer customer1 = new Customer(999, "BMW");
		Customer customer2 = new Customer(888, "FORD");
		Customer customer3 = new Customer(888, "FORD");
		System.out.println(customer1.equals(customer2));
		System.out.println(customer2.equals(customer3));
	}
}

Output:
false
false

Note: In this case Object class .equals() has executed which is meant for reference comparison but based on our requirement it is recommended to override .equals() for content comparison.

By overriding .equals() we have to consider the following 3 cases

  1. Case1: The meaning of equality
  2. Case2: In the case of heterogeneous objects we have to return false. (i.e) we have to handle ClassCastException to return false.
  3. Case3: If we are passing null as the argument we have return false. (i.e) we have to handle NullPointerException to return false.

Overriding equals method:
Example:



public class Customer {

	private int customerID;
	private String customerName;

	public Customer(int customerID, String customerName) {

		this.customerID = customerID;
		this.customerName = customerName;
	}

	public boolean equals(Object obj) {
		try {
			int customerID1 = this.customerID;
			String customerName1 = this.customerName;
			
			Customer s2 = (Customer) obj;
			int customerID2 = s2.customerID;
			String customerName2 = s2.customerName;
			
			if (customerName1.equals(customerName2)
					&& customerID1 == customerID2) {
				return true;
			} else {
				return false;
			}
		} catch (ClassCastException c) {
			return false;
		} catch (NullPointerException e) {
			return false;
		}
	}

	public static void main(String[] args) {

		Customer customer1 = new Customer(999, "BMW");
		Customer customer2 = new Customer(888, "FORD");
		Customer customer3 = new Customer(888, "FORD");
		System.out.println(customer1.equals(customer2));
		System.out.println(customer2.equals(customer3));
	}
}
/*
Output:
false
true
*/

clone() method implementation and usage:

The process of creating exactly duplicate Object is called Cloning.Object class contains the clone method to perform cloning of an object.

protected native Object clone() throws CloneNotSupportedException.

Example:

public class CloneDemo implements Cloneable {

	private int weight=70;
	private int height=12;
	
	public static void main(String[] args)throws CloneNotSupportedException {
	
		CloneDemo cloneDemo=new CloneDemo();
		CloneDemo cloneDemo2=(CloneDemo)cloneDemo.clone();
		cloneDemo2.weight=80;
		cloneDemo2.height=13;
		System.out.println(cloneDemo2.weight+"::"+cloneDemo2.height);
	}
}
/*
Output:
80::13
*/
  1. All the Objects can’t have the capability to produce cloned Objects. Only clonaeble objects having that capability.
  2. An Object is said to be cloneable iff the corresponding class has to implement java.lang.cloneable interface.
  3. Cloneable interface doesn’t contain any methods it is a marker interface.