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

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

Moderator: admin

Post Reply
Nisim123
Posts: 42
Joined: Mon Jan 20, 2014 2:26 pm
Contact:

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

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

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

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

Post 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.
If you like our products and services, please help us by posting your review here.

gparLondon
Posts: 63
Joined: Fri Oct 31, 2014 6:31 pm
Contact:

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

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

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

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

Post 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.
If you like our products and services, please help us by posting your review here.

abhimita
Posts: 5
Joined: Sun Jun 21, 2015 12:54 am
Contact:

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

Post by abhimita »

I don't quite fully understand the question properly. Would you please explain it?

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

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

Post by admin »

Can you please tell which part do you not understand?
If you like our products and services, please help us by posting your review here.

abhimita
Posts: 5
Joined: Sun Jun 21, 2015 12:54 am
Contact:

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

Post 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;
}
}

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

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

Post 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.
If you like our products and services, please help us by posting your review here.

abhimita
Posts: 5
Joined: Sun Jun 21, 2015 12:54 am
Contact:

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

Post by abhimita »

I get it now. Thank you. sorry for the bad example.

girishankaran
Posts: 10
Joined: Wed Aug 08, 2012 5:00 am
Contact:

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

Post 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);
}
}
}

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

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

Post by admin »

You are using the same object in your code! Please read the question carefully.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 59 guests