public class Main {
public static void main
(String[] args
) {
C c = new C("Test", 10);
D d = new D("Test", 10);
if (c.equals(d))
else
System.
out.
println("Unequal");
if (d.equals(c))
else
System.
out.
println("Unequal"); }
}
class C
{
str = cstr;
num = cnum;
}
@Override
public boolean equals
(Object otherObject
) {
// A quick test to see if the objects are identical.
if (this == otherObject) {
return true;
}
// Must return false if the explicit parameter is null
if (otherObject == null)
{
return false;
}
if (!(otherObject instanceof C))
return false;
// Now we know otherObject is a non-null Employee
C other = (C) otherObject;
// Test whether the fields have identical values
return str.equals(other.str) && num == other.num;
}
private int num;
}
class D extends C {
super(cstr, cnum);
}
}
ICAgIHB1YmxpYyBjbGFzcyBNYWluIHsKICAgIAogICAgCXB1YmxpYyBzdGF0aWMgdm9pZCBtYWluKFN0cmluZ1tdIGFyZ3MpIHsKICAgIAkJCiAgICAJCUMgYyA9IG5ldyBDKCJUZXN0IiwgMTApOwogICAgCQlEIGQgPSBuZXcgRCgiVGVzdCIsIDEwKTsKICAgIAkJCiAgICAJCWlmIChjLmVxdWFscyhkKSkKICAgIAkJCVN5c3RlbS5vdXQucHJpbnRsbigiRXF1YWwiKTsKICAgIAkJZWxzZQogICAgCQkJU3lzdGVtLm91dC5wcmludGxuKCJVbmVxdWFsIik7CiAgICAJCQogICAgCQlpZiAoZC5lcXVhbHMoYykpCiAgICAJCQlTeXN0ZW0ub3V0LnByaW50bG4oIkVxdWFsIik7CiAgICAJCWVsc2UKICAgIAkJCVN5c3RlbS5vdXQucHJpbnRsbigiVW5lcXVhbCIpOwogICAgCX0KICAgIH0KICAgIAogICAgCiAgICBjbGFzcyBDCiAgICB7CiAgICAJQyhTdHJpbmcgY3N0ciwgaW50IGNudW0pIHsKICAgIAkJc3RyID0gY3N0cjsKICAgIAkJbnVtID0gY251bTsKICAgIAl9CiAgICAJCiAgICAJQE92ZXJyaWRlCiAgICAJcHVibGljIGJvb2xlYW4gZXF1YWxzKE9iamVjdCBvdGhlck9iamVjdCkgewogICAgCiAgICAJCS8vIEEgcXVpY2sgdGVzdCB0byBzZWUgaWYgdGhlIG9iamVjdHMgYXJlIGlkZW50aWNhbC4KICAgIAkJaWYgKHRoaXMgPT0gb3RoZXJPYmplY3QpIHsKICAgIAkJCXJldHVybiB0cnVlOwogICAgCQl9CiAgICAKICAgIAkJLy8gTXVzdCByZXR1cm4gZmFsc2UgaWYgdGhlIGV4cGxpY2l0IHBhcmFtZXRlciBpcyBudWxsCiAgICAJCWlmIChvdGhlck9iamVjdCA9PSBudWxsKQogICAgCQl7CiAgICAJCQlyZXR1cm4gZmFsc2U7CiAgICAJCX0KICAgIAogICAgCQlpZiAoIShvdGhlck9iamVjdCBpbnN0YW5jZW9mIEMpKQogICAgCQkJcmV0dXJuIGZhbHNlOwogICAgCQkKICAgIAkJLy8gTm93IHdlIGtub3cgb3RoZXJPYmplY3QgaXMgYSBub24tbnVsbCBFbXBsb3llZQogICAgCQlDIG90aGVyID0gKEMpIG90aGVyT2JqZWN0OwogICAgCiAgICAJCS8vIFRlc3Qgd2hldGhlciB0aGUgZmllbGRzIGhhdmUgaWRlbnRpY2FsIHZhbHVlcwogICAgCQlyZXR1cm4gc3RyLmVxdWFscyhvdGhlci5zdHIpICYmIG51bSA9PSBvdGhlci5udW07CiAgICAJfQogICAgCQogICAgCXByaXZhdGUgU3RyaW5nIHN0cjsKICAgIAlwcml2YXRlIGludCBudW07CiAgICB9CiAgICAKICAgIGNsYXNzIEQgZXh0ZW5kcyBDIHsKICAgIAkJCiAgICAJRChTdHJpbmcgY3N0ciwgaW50IGNudW0pIHsKICAgIAkJc3VwZXIoY3N0ciwgY251bSk7CiAgICAJfQogICAgfQ==