Example of Object class

We know that Object class is default super class  for all the java classes i.e without extending object class we can  access the all methods of Object class.Object class belongs to java.lang package.
There are some method of Object class are getClass(), hashCode, toString(),equals(),wait() and more .


Here my purpose is show the example of Object class with overriding some methods like hashCode,equals() and toString().


Suppose that a package:com.javaforecast in which there are two class one is
 Customer.java  : In this class we override the there method of Object class are hashCode, toString and equals()
and class is  
TestExampleObject.java : In this class we create the object of Customer.java class


Now see the code 


Customer.java



package com.javaforcast;

public class Customer {
int cid;
String cname;
String email;
long phone;
Customer(){}
Customer(int cid,String cname,String email,long phone){
 this.cid=cid;
 this.cname=cname;
 this.email=email;
 this.phone=phone;

}
//method for showing customer information
void show(){
 System.out.println(cid+"\t"+cname+"\t"+email+"/t"+phone);

}
//here overriding the hashCode of Object class method
public int hashCode(){
 System.out.println("inside hashCode()...");
 int x=20;
 return x;
}//end of hashCode
//here overriding equals method of Object class..

public boolean equals(Object o){
 Customer c=(Customer)o;
 if(this.cid==c.cid && this.cname.equals(c.cname)){
  return true;
 }
 return false;
  }//end of equals

//override toString method
public String toString(){
 return ""+cid+"\t"+cname+"\t"+email+"\t"+phone;
 }
}


TestExampleObject.java


/**
  * @Author:javaforcast
  * Example of Object class
  */

package com.javaforcast;

public class TestExampleObject {

  public static void main(String[] args) {
  // TODO Auto-generated method stub
   Customer c1 =new Customer(100,"Arbind","arbind.sharma2@gmail.com",83838373);
   Customer c2 =new Customer(101,"Addy","arbind.sharma2@gmail.com",83838373);
   Customer c3 =new Customer(102,"Sharma","arbind.sharma2@gmail.com",83838373);
   c1.show();
   System.out.println(c1.getClass());
   System.out.println(c1.hashCode());
   System.out.println(c1.toString());
   System.out.println(c2);
   System.out.println(c1);
   System.out.println(c1==c2);
   System.out.println(c1.equals(c2));
   System.out.println(c1==c3);
   System.out.println(c1.equals(c3));
  

 }

}


Output :


100 Arbind arbind.sharma2@gmail.com/t83838373

class com.javaforcast.Customer

inside hashCode()...

20

100 Arbind arbind.sharma2@gmail.com 83838373

101 Addy arbind.sharma2@gmail.com 83838373

100 Arbind arbind.sharma2@gmail.com 83838373

false

false
false
false