About Question enthuware.ocpjp.v17.2.3699 :

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

Moderator: admin

Post Reply
i.salvadori
Posts: 4
Joined: Thu Dec 01, 2022 3:27 am
Contact:

About Question enthuware.ocpjp.v17.2.3699 :

Post 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

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

Re: About Question enthuware.ocpjp.v17.2.3699 :

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

i.salvadori
Posts: 4
Joined: Thu Dec 01, 2022 3:27 am
Contact:

Re: About Question enthuware.ocpjp.v17.2.3699 :

Post 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

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

Re: About Question enthuware.ocpjp.v17.2.3699 :

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

i.salvadori
Posts: 4
Joined: Thu Dec 01, 2022 3:27 am
Contact:

Re: About Question enthuware.ocpjp.v17.2.3699 :

Post by i.salvadori »

Point 2 was not clear to me. Now it is.

Thanks

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 11 guests