About Question enthuware.ocpjp.v7.2.1098 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
profnotime
Posts: 3
Joined: Sat Jun 28, 2014 10:29 am
Contact:

About Question enthuware.ocpjp.v7.2.1098 :

Post by profnotime »

Please explain how option three satisfies the equals contract. I am confused :?:

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

Can you show me a situation where it breaks the contract?

colmkav
Posts: 21
Joined: Thu Jul 16, 2015 4:22 am
Contact:

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

Post 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?

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

Why do you think this and tc will always refer to objects of different classes?

colmkav
Posts: 21
Joined: Thu Jul 16, 2015 4:22 am
Contact:

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

Post 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.

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.

pbonito
Posts: 13
Joined: Sat May 16, 2015 12:38 pm
Contact:

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

Post 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?

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

I agree it is useless but it is still valid, which is what this question cares about.

Post Reply

Who is online

Users browsing this forum: No registered users and 12 guests