
About Question enthuware.ocpjp.v7.2.1098 :
Moderator: admin
-
- Posts: 3
- Joined: Sat Jun 28, 2014 10:29 am
- Contact:
About Question enthuware.ocpjp.v7.2.1098 :
Please explain how option three satisfies the equals contract. I am confused 

-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1098 :
Can you show me a situation where it breaks the contract?
-
- Posts: 21
- Joined: Thu Jul 16, 2015 4:22 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1098 :
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?
Why is this not the case?
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1098 :
Why do you think this and tc will always refer to objects of different classes?
-
- Posts: 21
- Joined: Thu Jul 16, 2015 4:22 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1098 :
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 wrote:Why do you think this and tc will always refer to objects of different classes?
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1098 :
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.
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.
-
- Posts: 13
- Joined: Sat May 16, 2015 12:38 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1098 :
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?
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?
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1098 :
I agree it is useless but it is still valid, which is what this question cares about.
Who is online
Users browsing this forum: No registered users and 23 guests