Equals Method on Cloned objects | Core Java Forum
A
Aditya Bhattacharya Posted on 25/02/2020

public class CloneMethodUsage implements Cloneable{

int attribute1;
String attribute2;

CloneMethodUsage(int attribute1, String attribute2){
this.attribute1 = attribute1;
this.attribute2 = attribute2;
}

public static void main(String[] args) throws CloneNotSupportedException {
CloneMethodUsage obj1 = new CloneMethodUsage(1, "Aditya");

CloneMethodUsage obj2 = (CloneMethodUsage) obj1.clone();
System.out.println("First attribute value = " + obj1.attribute1);
System.out.println("Second attribute value = " + obj1.attribute2);
System.out.println("Clone Successfull.....");
System.out.println("First attribute value = " + obj2.attribute1);
System.out.println("Second attribute value = " + obj2.attribute2);

if(obj1.equals(obj2)) {
System.out.println("The objects are cloned");
}else {
System.out.println("Objects are not cloned");
}

}

}

As obj2 is a clone of obj1 and both belong to the same class, the equals operator fails in this case. Can you please explain?

 


Y
Yogesh Chawla Replied on 25/02/2020

As we discussed during the class, in case of custom or user defined class, creation of equals method is must. I also created it from the IDE itself. 

Source -> Generate hashCode and equals method option for all fields.

 

Even if this methods exist somewhere in the hierarchy, any parent class has, that will also work just that the equals method implementation should be there.

In the class itself/anywhere in the parent abstract class or anywhere in the base framework class if we are working on a framework, if equals method implementation is there, this concept will work.

public class Test implements Cloneable{
	int attribute1;
	String attribute2;

	Test(int attribute1, String attribute2) {
		this.attribute1 = attribute1;
		this.attribute2 = attribute2;
	}

	public static void main(String[] args) throws CloneNotSupportedException {
		Test obj1 = new Test(1, "Aditya");

		Test obj2 = (Test) obj1.clone();
		System.out.println("First attribute value = " + obj1.attribute1);
		System.out.println("Second attribute value = " + obj1.attribute2);
		System.out.println("Clone Successfull.....");
		System.out.println("First attribute value = " + obj2.attribute1);
		System.out.println("Second attribute value = " + obj2.attribute2);

		if (obj1.equals(obj2)) {
			System.out.println("The objects are equal");
		} else {
			System.out.println("Objects are not equal");
		}

	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + attribute1;
		result = prime * result + ((attribute2 == null) ? 0 : attribute2.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;
		Test other = (Test) obj;
		if (attribute1 != other.attribute1)
			return false;
		if (attribute2 == null) {
			if (other.attribute2 != null)
				return false;
		} else if (!attribute2.equals(other.attribute2))
			return false;
		return true;
	}

}