Page 1 of 1

About Question enthuware.ocpjp.v7.2.1098 :

Posted: Mon Jun 30, 2014 2:25 pm
by profnotime
Please explain how option three satisfies the equals contract. I am confused :?:

Re: About Question enthuware.ocpjp.v7.2.1098 :

Posted: Mon Jun 30, 2014 8:14 pm
by admin
Can you show me a situation where it breaks the contract?

Re: About Question enthuware.ocpjp.v7.2.1098 :

Posted: Wed Aug 12, 2015 3:01 pm
by colmkav
I thought option 3 would always return false because I thought "this" would be of reference type, TestClass. As this and tc refer to different class types, this == tc should always return false.

Why is this not the case?

Re: About Question enthuware.ocpjp.v7.2.1098 :

Posted: Wed Aug 12, 2015 11:09 pm
by admin
Why do you think this and tc will always refer to objects of different classes?

Re: About Question enthuware.ocpjp.v7.2.1098 :

Posted: Thu Aug 13, 2015 3:02 am
by colmkav
admin wrote:Why do you think this and tc will always refer to objects of different classes?
I suppose I am thinking that 2 reference variables (an object reference and a TestClass reference) can not be deemed equal as they are different types but I suppose it is the object that they point to that is compared and they can (via polymorphism?) point to the same object.

Re: About Question enthuware.ocpjp.v7.2.1098 :

Posted: Thu Aug 13, 2015 4:38 am
by admin
Yes, two references can point to the same object. For example:
TestClass t = new TestClass();
Object o = t; //now, o and t both point to the same TestClass object.
If you pass o to the equals method, it should return true.

Re: About Question enthuware.ocpjp.v7.2.1098 :

Posted: Tue Oct 13, 2015 10:04 am
by pbonito
public boolean equals(Object tc) {    return this == tc; }

More than valid this seems to be useless since it seems to be the implementation already present in Object class.
Maybe the last option is a better choice? Can we consider valid a method with such override?

Re: About Question enthuware.ocpjp.v7.2.1098 :

Posted: Tue Oct 13, 2015 11:46 am
by admin
I agree it is useless but it is still valid, which is what this question cares about.