Page 1 of 1

About Question enthuware.ocajp.i.v7.2.1206 :

Posted: Fri Jul 04, 2014 5:41 am
by Nisim123
Somewhere deep in an explanation of a different question in a different test,
it was explained, please correct me if i am wrong, that the first thing that the equals() method does is to check if both objects
belong to the same class...(or extending each other. Probably using the instaceof operator....)
:roll:

Re: About Question enthuware.ocajp.i.v7.2.1206 :

Posted: Fri Jul 04, 2014 5:56 am
by admin
In question 2.1125, the explanation says, "The equals methods of all wrapper classes first check if the two object are of same class or not. If not, they immediately return false", which is correct.

You can see the code here: http://www.docjar.com/html/api/java/lan ... .java.html

If you saw something else, please let me know the question id and I will take a look.

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.1206 :

Posted: Sat Jan 17, 2015 3:29 pm
by gparLondon
So, it is possible that an equals method may return true even if the class of the passed object has no relation to this object.
But, in that case, its a violation of contract of the equals method overriding, on such questions cant we assume that, programmer wont violate the contract?

Re: About Question enthuware.ocajp.i.v7.2.1206 :

Posted: Sat Jan 17, 2015 9:15 pm
by admin
No, it wouldn't necessarily be a violation of the equals contract. The contract says that it needs to be reflexive, symmetric, transient, and consistent. You can satisfy these even with two different classes.

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.1206 :

Posted: Mon Jun 22, 2015 5:25 am
by abhimita
I don't quite fully understand the question properly. Would you please explain it?

Re: About Question enthuware.ocajp.i.v7.2.1206 :

Posted: Mon Jun 22, 2015 7:54 pm
by admin
Can you please tell which part do you not understand?

Re: About Question enthuware.ocajp.i.v7.2.1206 :

Posted: Mon Jun 22, 2015 10:52 pm
by abhimita
If a.equals(b) returns true, b instanceof ClassOfA must always be true. (Assume that ClassOfA is the name of the class of the variable a.)

Is this how I should interpret the questions as? Please correct me if m wrong
public ClassOfA {
public int a;

}
public ClassOfB extends ClassOfA{
ClassOfA b = new ClassOfB;

if(a.equals(b)){
returns true;
}
}

Re: About Question enthuware.ocajp.i.v7.2.1206 :

Posted: Mon Jun 22, 2015 11:57 pm
by admin
From the statement, the first part tells you that a.equals(b) returns true. While the code that you've given will not even compile. So I am not sure how are you interpreting.
a and b should be variables of some class (not of type int!) such that a.equals(b) return true. You can now think of cases where this holds true and then determine whether b instanceof ClassOfA will also return true or not. ClassOfA is whatever class you choose.

Re: About Question enthuware.ocajp.i.v7.2.1206 :

Posted: Tue Jun 23, 2015 5:28 am
by abhimita
I get it now. Thank you. sorry for the bad example.

Re: About Question enthuware.ocajp.i.v7.2.1206 :

Posted: Mon Feb 03, 2020 8:52 am
by girishankaran
I could not understand the problem, its both return true

class ClassOfA {

}
class ClassOfB extends ClassOfA{
}

public class TestClassAB {

public static void main(String[] args) {
ClassOfA a = new ClassOfA();
ClassOfA b =a;

if(a.equals(b)){
System.out.println("true");
System.out.println(b instanceof ClassOfA);
}
}
}

Re: About Question enthuware.ocajp.i.v7.2.1206 :

Posted: Mon Feb 03, 2020 8:57 am
by admin
You are using the same object in your code! Please read the question carefully.