Page 1 of 1

About Question enthuware.ocpjp.ii.v11.2.3402 :

Posted: Sat Mar 07, 2020 10:22 am
by FlatPanda
Based on this question: we only should assume that a question is about immutability, when it is explicitly stated? I know that in order to make this class immutable there is a lot of other things to do, but I still was thrown off-track because of thinking about immutability.

Re: About Question enthuware.ocpjp.ii.v11.2.3402 :

Posted: Sat Mar 07, 2020 10:30 pm
by admin
Unfortunately, the questions on this topic in the exam are somewhat ambiguous. The right option(s) depend not only on technical correctness but also on the number of correct options that you have to select. You need to select only the best option(s) even if you find another to be also technically correct. For this reason, it is not possible to provide a firm rule that can deduce the correct answer.

In this question, I think "Make the class final" should also be a correct option.

Paul.

Re: About Question enthuware.ocpjp.ii.v11.2.3402 :

Posted: Mon Oct 19, 2020 2:40 am
by kabanvau
Validation of dob must be done after cloning. Am I right?

Re: About Question enthuware.ocpjp.ii.v11.2.3402 :

Posted: Mon Oct 19, 2020 5:56 am
by admin
Yes, you are right. validation should be done after cloning.

Re: About Question enthuware.ocpjp.ii.v11.2.3402 :

Posted: Thu Nov 19, 2020 10:05 pm
by olsongrant
My experience with this question was that it just said "Given <this code>", and then it provided answer choices that were in the form of "should" statements.

The answers considered correct are that (1) the protected modifier on validateDob should be changed to private, to prevent subclasses from messing with the validation behavior, and (2) the class should be made final. When I looked at these two in conjunction, I perceived the privatization of the validateDob method to be redundant with the making of the class final (from a don't-mess-with-the-behavior perspective), so I figured that those two choices wouldn't together form a coherent answer. (Maybe I still need to learn something about what it means for a class to be final?) Meanwhile, I don't know that we can automatically assume that Person should be an immutable, final class. If I were building a system that involved a Person class, I'd be tempted to make subtypes for different roles.
I don't want to assume I know the intent of the question, but my purpose for writing is just to suggest that there might be some improvements to make by clarifying the context and intended usage of the Person class.

Re: About Question enthuware.ocpjp.ii.v11.2.3402 :

Posted: Thu Nov 19, 2020 10:18 pm
by admin
Yes, your argument is correct. The problem statement is missing the statement:
"What changes should be made to make this class immutable?"

Regarding making the class final, again, you are right. There are several possibilities in real life, but for the purpose of the exam, we need to pick the options based on Java Security guidelines. So, based on that, option 2 and 3 are valid.

Re: About Question enthuware.ocpjp.ii.v11.2.3402 :

Posted: Thu Dec 31, 2020 4:42 am
by 2014098616
Hello Admin, I also think making the validateDob method final still protect the creation of the object as subclasses will not be able to override the method, moreover, the method itself, even if subclasses were allowed to invoke it, does not seem to interfere with the object state. I also agree that making the method private solves the same problem as the subclasses do not see the method.

Re: About Question enthuware.ocpjp.ii.v11.2.3402 :

Posted: Sat Jan 02, 2021 12:26 am
by admin
1. validateDOB should be private because it is being invoked from a constructor.
2. The class should be made final because the code shows " //other methods not shown" and it is recommended for an immutable class to be final.

So, in our view, for the purpose of this exam, if you have to select 2 options, both the options should be selected.

Re: About Question enthuware.ocpjp.ii.v11.2.3402 :

Posted: Fri Jan 08, 2021 11:58 pm
by philippe
In the following security guideline: https://www.oracle.com/java/technologie ... e.html#6-1
it is written that:
Immutable classes themselves should declare fields final.
Why is option 4:
name and dob fields should be made final.
not a correct option?

Re: About Question enthuware.ocpjp.ii.v11.2.3402 :

Posted: Sat Jan 09, 2021 1:01 am
by admin
Yes, ideally the fields should be final as well. Unfortunately, the questions on this topic tend to be somewhat ambiguous because the code given in the question is either not complete or is not realistic. So, the same thing can be achieve with different approaches.
For example, in this question, the focus is on invoking a non-final non-private method from a constructor. Since no other code is shown to modify the fields, the class is still immutable if the fields are not final, which, of course, violates guideline 6.1

So, the point is that, answers to such questions may be debated forever. It depends on what the question writer at Oracle thinks is the right answer. It is best to use your judgement based on the number of correct options that you have to select.

Re: About Question enthuware.ocpjp.ii.v11.2.3402 :

Posted: Mon Jan 24, 2022 10:50 am
by brintal
I'm also on the "Option 4 should be correct"-Team.

Because Option 3 "Person class should be made final." is enough to prevent subclasses that might override validateDob.
Hence Option 2 is unnecessary (because overriding validateDob is not possible anyways) and Option 4 makes much more sense in combination with Option 3.