public boolean equals (object Otherobject) { if (this = otherobject) { //checks if this and Otherobject refer to the same object return true; } if (null = = Otherobject) { //check if Otherobject is empty return false; } if (! ( GetClass () = = Otherobject.getclass ())) { //Compare this to oherobject whether it belongs to the same class, assuming that the semantics of equal have changed in each of the subclasses, use this inference System.out.println ("-----------------getclass----------------"); return false; } if (! (Otherobject instanceof Apple)) { //Assuming semantics is also inferred using instanceof, System.out.println ("------------instanceof--------------------") is also used to infer inheritance; return false; } Apple other = (apple) otherobject; Convert to the corresponding type. Infer the desired domain return name.equals (other.name) && color.equals (Other.color); }}
Java Perfect equals method code snippet