About Question enthuware.ocpjp.v7.2.1297 :

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

Moderator: admin

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

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

Post by colmkav »

admin wrote:Explanation is talking about equals(Resource ) because that is how the method is coded in the given code. This method is causing the problem discussed in the explanation and above.
I am still not sure what you mean by "should be used". Do you mean the JVM should automatically figure out which method should be used or do you mean the developer should code only equals(Object ) method and should not code equals(Resource )?

Anyway, the fact is that the given code contains equals(Resource ) method and the question expects you to determine what will happen. The explanation explains exactly what happens. Please go through it.

-Paul.
I understand the explanation. I just disagree with the assumption made in it. Why would we assume to be in the com.enthu package? If we do the comparison outside the package then we clearly use the "usual" equals method using Object as a parameter. The answer is still the same so its an academic point.

"if two distinct Resource objects are considered equal, it means that the comparison has been done from a class that belongs to com.enthu package"

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

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

Post by admin »

No, it is not an academic point. If you do the comparison from outside com.enthu package, two distinct Resource objects will NOT be considered equal. That is the whole point of the question!
You have not understood the explanation. I would suggest you to go through the whole explanation once more.
If you like our products and services, please help us by posting your review here.

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

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

Post by colmkav »

admin wrote:No, it is not an academic point. If you do the comparison from outside com.enthu package, two distinct Resource objects will NOT be considered equal. That is the whole point of the question!
You have not understood the explanation. I would suggest you to go through the whole explanation once more.
Ah ok, it does say 2 "distinct" objects. I understand. thx

Ad9999
Posts: 15
Joined: Fri May 15, 2015 12:06 am
Contact:

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

Post by Ad9999 »

Very good question. Didn't consider what would happen if you call equals from outside the package. And..

Surely :roll: , Mr.Admin displayed some legendary patience in this thread. :lol:

nsobchuk
Posts: 3
Joined: Sun Aug 17, 2014 7:59 am
Contact:

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

Post by nsobchuk »

I've played a little with this question and I've found out something strange about this equals() overriding.
I've created two classes:

Code: Select all

public class Resource {
    private String data = "DATA";

    boolean equals(Resource r) { //LINE 1
        return (r != null && r.data.equals(this.data));
    }
}

Code: Select all

public class EqualsTest {
    public static void main(String[] args) {
        System.out.println(new Resource().equals(new Resource())); //LINE 2
    }
}
So forth, here's what I have found out:
1. If Both classes reside in the same package - equals(Resource r) of Resource class is called. I'm fine with that.
2. If classes reside in the different packages - Object's equals(Object o) method is called. I'm fine with that also.
3. If I change LINE 2 to be public boolean equals(Resource r) - it doesn't get called. That's not fine. Why wouldn't it? It's an exact match, more precise than equals(Object o) isn't it? Nevertheless, if I change LINE 2 to the following:

Code: Select all

System.out.println(new Resource().equals((Resource)new Resource())); //LINE 2
- public boolean equals(Resource r) get's called.

Could anyone explain me what is happening there? Why wouldn't equals match the exact match without an explicit cast?

UPD: This could be some IDEA related bug. I've tried playing some more with it and it behaves unpredictably :shock:

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

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

Post by admin »

If you make it public, it should indeed call Resource's equals method. Please double check what you are doing from command line instead of IDE.
-Paul.
If you like our products and services, please help us by posting your review here.

jagoneye
Posts: 97
Joined: Wed Dec 28, 2016 9:00 am
Contact:

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

Post by jagoneye »

The king kong question of all! The assumption to be made in order to mark the third option was skyline! Well done whoever designed this question!
:thumbup:

codingrobot
Posts: 1
Joined: Thu Mar 30, 2017 7:10 pm
Contact:

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

Post by codingrobot »

Yet another "why on earth" moment...

This and many other mock questions for the OCP exam have indeed very "illogical" answers. I can only appreciate that we have such resources available to prepare for the exam. The explanations and discussions on the forum are extremely useful! Excellent work Enthuware!

As the OCP Study Guide (Boyarsky, Selikoff) says - Don't do such things in code someone is paying you to write :mrgreen:

Post Reply

Who is online

Users browsing this forum: No registered users and 125 guests