Wednesday, 3 September 2014

JAVA Hashcode, toString And equal method overriding

package com.mastermoin.java.bean;

public class UserBean {

       private int userid;
       private String userName;
      
       public UserBean(int userid , String userName) {
                     this.userid   = userid;
                     this.userName = userName;
       }
      
       public int getUserid() {
              return userid;
       }
       public void setUserid(int userid) {
              this.userid = userid;
       }
       public String getUserName() {
              return userName;
       }
       public void setUserName(String userName) {
              this.userName = userName;
       }
       @Override
       public int hashCode() {
              final int prime = 31;
              int result = 1;
              result = prime * result
                           + ((userName == null) ? 0 : userName.hashCode());
              return result;
       }
       @Override
       public boolean equals(Object obj) {
              if (this == obj)
                     return true;
              if (obj == null)
                     return false;
              if (getClass() != obj.getClass())
                     return false;
              UserBean other = (UserBean) obj;
              if (userName == null) {
                     if (other.userName != null)
                           return false;
              } else if (!userName.equals(other.userName))
                     return false;
              return true;
       }
       @Override
       public String toString() {
              return "UserBean [userName=" + userName + "]";
       }
             
}

________________________________________________
Test All method as 

package com.mastermoin.java;

import com.mastermoin.core.SingletonClass;
import com.mastermoin.java.bean.UserBean;

public class DemoRun {

       public static void main(String[] args) {

              UserBean obj1 = new UserBean(1,"moin");
              UserBean obj2 = new UserBean(2,"moin");
              UserBean obj3 = new UserBean(3,"master");
             
              if (obj1.equals(obj2)){
                     System.out.println("both objects are same");
              }

              if (obj1.equals(obj3)){
                     System.out.println("both objects are same");
              }else{
                     System.out.println("both objects are not same");
              }
       }
}

__________________________________________________________
output

both objects are same
both objects are not same

__________________________________________________________

cover all class member in highlighted condition as per business logic to check object equality 

if we cover userid in highlighted condition than we will get obj1 and obj2 are not same object because there exist userid of both objects are different 1 and 2 but we are only concern with user name, so we place only userName in highlighted condition.


No comments:

Post a Comment