Page 1 of 1
About Question enthuware.ocpjp.v17.2.3699 :
Posted: Thu Dec 08, 2022 1:41 am
by i.salvadori
Hi,
I have a question here. The correct options for this question are
Code: Select all
public final boolean equals(Object)
public final int hashCode()
public final String toString()
Are we sure that these methods are final?
I am studying on the OCP Study Guide from Selikoff and Boyarsky and there the authors say that you can override the methods that are inserted automatically in a record definition, including equals(), hashCode() and toString(). However, in order to be overridden they cannot be defined as final.
Or am I missing something here?
Thank you for the help!
Cheers,
Ilenia
Re: About Question enthuware.ocpjp.v17.2.3699 :
Posted: Thu Dec 08, 2022 2:58 am
by admin
There are two things:
1. The question is asking about the methods that are added automatically (i.e. by the compiler). The correct options specify those methods and those methods are indeed final. The question is not asking about the modifier while overriding these methods. It is asking about the methods that are provided by the compiler automatically.
2. Even if you want to override these methods, you can definitely mark them final. For example, the following code compiles fine -
Code: Select all
record Student(int id, String name) {
public final String toString(){ //compiles fine with or without final modifier here
return "something";
}
}
You may try it out.
So if the book that you are referring to says something else, you need to contact the authors and check with them.
HTH,
Paul.
Re: About Question enthuware.ocpjp.v17.2.3699 :
Posted: Thu Dec 08, 2022 4:02 am
by i.salvadori
Hi,
I agree with the fact that when you override those methods you can mark them as final.
But my point is, how can they be overridden in the first place if the ones that are automatically inserted are final? You cannot override a final method, so it should not be possible to override them if the ones that the compiler insert are final.
Thanks again,
Ilenia
Re: About Question enthuware.ocpjp.v17.2.3699 :
Posted: Fri Dec 09, 2022 7:53 am
by admin
Again, I am not sure what the book says, so I cannot comment on that. But,
1. The problem statement and explanation of our question is correct because it is talking about the method inserted by the compiler.
2. These methods are already defined in Object class. So, if you provide these methods explicitly in your record class, you are actually overriding them. So I am not sure what is the issue.
Re: About Question enthuware.ocpjp.v17.2.3699 :
Posted: Fri Dec 09, 2022 8:03 am
by i.salvadori
Point 2 was not clear to me. Now it is.
Thanks