Apologies ...
The example provided does not provide the complete solution. Actually equals() method a method which would check the hashcode of the object and since all the class inherit either directly or indirectly so they do inherit this method.
Since String class has overridden this method such that equals() method checks for the content and hence it would work but if u have any other class we need to override this method according to our need : I am quoting the example provided in the Thinking in
Java : ( An excellent book for concepts )
class Value {
int i;
}
public class EqualsMethod2 {
static
Test monitor = new Test();
public static void main(String[] args) {
Value v1 = new Value();
Value v2 = new Value();
v1.i = v2.i = 100;
System.out.println(v1.equals(v2));
});
}
}
See the difference in results when we have String and any other class.
Cheers,
Murali