For example:
public class Fruit{ private String color; private double weight; ... public boolean equals(Fruit a){ return (this.getColor().equals(a.getColor()) && (this.getWeight()==a.getWeight())); } } public class Orange extends Fruit{ public Orange(double weight) { super("orange", weight); } } public class NavalOrange extends Orange{ public NavalOrange(double weight) { super(weight); } } Then I wrote a test about this to check two objects if they are same.
Orange orange = new Orange(8); NavalOrange navalOrange = new NavalOrange(8); assertEquals(orange, navalOrange); But it just keeps returning errors:
junit.framework.AssertionFailedError: expected:<Orange@5305068a> but was:<NavalOrange@1f32e575> Can anyone explain why this is happening? I think it may be something wrong at the toString method.
Fruitclass and selectingGenerate equals() and hashcode().