As Java uses implicit references you need to make a difference between object euqality and reference euqality.
If you just want to know wether two objects denode the same memory cell, so they are really the same, you cann use the equals operator:
Object A = new Fruit("A");
Object B = new Fruit("A");
System.out.println(A == B);
This would print false, as A and B are not the same reference cell.
Object A = new Fruit("A");
Object B = A
System.out.println(A == B);
Would return true, as they both are "pointers" to the same reference cell.
If you want to achieve semantic equality, I would advise you to make use of the equals method AND the fields of the class.
Object A = new Fruit("A");
Object B = new Fruit("A");
System.out.println(A.equals(B));
should return true in this case.
To achieve this you can usethe following equals method for every possible class you could write: (assuming you have getter and setter for the field A and your class is named myClass)
public boolean equals(Object B){
if(B instanceof MyClass){
MyClass B = (MyClass) B;
boolean flag = this.getA().equals(B.getA());
return flag;
}
return false;
}
You would have to do the boolean flag = this.getA().equals(B.getA());
for every field of the class.
This would lead to equality if the objects fields have the same values.
But oyu have to keep in mind, that there is no perfect equals method. There is the so called hashcode equals contract in java which means that A.equals(B)
has to hold, whenever A.hashCode()==B.hashCode()
A nice thing of the flag approach is that you don't have to care about the types of the objects fields, as for Basic Types that are not Objects (int, float, ...) the Java Runtime will do implicit bocing and cast them to Integer or Float Objects and use the equals method of them.