Page 1 of 1
About Question enthuware.ocajp.i.v7.2.1113 :
Posted: Wed May 21, 2014 8:31 am
by bptoth
Perhaps if the question was put in the form of "Select the best option", it could be detailed why the chosen answer regarding covariant types should be considered "better" than plain Object (for me it is quite much context dependent what to use, so I do not consider this a very good phrasing, but maybe there are similar questions in the real exam as well?)
Re: About Question enthuware.ocajp.i.v7.2.1113 :
Posted: Wed May 21, 2014 9:34 am
by admin
The problem statement does say, "(Select the best option.)".
Re: About Question enthuware.ocajp.i.v7.2.1113 :
Posted: Sun Jul 13, 2014 4:41 pm
by Brian B
After reading the explanation, I agree with the answer. However, I think "Select the best option" is subjective. Wouldn't both String and Object be technically valid answers? Would what is considered the "best option" be based on the purpose of the code being written?
As far as reinforcing the concept this question is trying to teach, would it make more sense if it allowed selection of multiple answers? Perhaps including primitives in the choices would help to clarify the point that covariance doesn't apply to them.
Re: About Question enthuware.ocajp.i.v7.2.1113 :
Posted: Sun Jul 13, 2014 10:02 pm
by admin
Yes, they are valid options. However, there will be situations in the exam where multiple valid options may be present but you have to pick only one. So you have to select the option that is most suitable.
You are right about primitives. The explanation has been enhanced to explain that as well.
Paul.
Re: About Question enthuware.ocajp.i.v7.2.1113 :
Posted: Tue Aug 06, 2024 7:42 am
by raphaelzintec
i hope i wont have Oracle error by saying: Return type can be any class since all objects can be cast to Object.
because imagine Oracle says you are wrong it cannot be anytype such as primitive
Re: About Question enthuware.ocajp.i.v7.2.1113 :
Posted: Thu Aug 29, 2024 4:09 am
by Newtoned
bptoth wrote: ↑Wed May 21, 2014 8:31 am
Perhaps if the question was put in the form of "Select the
best option",
best sex dolls it could be detailed why the chosen answer regarding covariant types should be considered "better" than plain Object (for me it is quite much context dependent what to use, so I do not consider this a very good phrasing, but maybe there are similar questions in the real exam as well?)
In the context of an exam or coding best practices, covariant types are often considered "better" than using Object because they offer a balance of flexibility and safety. However, the "best" option always depends on the specific use case and the context in which the code is being written.
Re: About Question enthuware.ocajp.i.v7.2.1113 :
Posted: Sun Feb 02, 2025 9:40 am
by n199a_
There is a question:
Which of the following is a legal return type of a method overriding the given method:
public Object myMethod() {...}
(Select the best option.)
The answer that you think is correct is the following:
Return type can be any class since all objects can be cast to Object.
The question clearly states that we should override the method first and then give the answer.
However, your
answer is fundamentally wrong for the reason that when overriding a method, it
IS POSSIBLE to change the type of the return value. Therefore, the return value cannot be any type of object.
Here is the proof:
Code: Select all
public interface Parent {
public Object myMethod();
}
Code: Select all
public class Main implements Parent {
@Override
public Long myMethod() {
return Integer.getInteger("1"); // Not compiler!
}
}
Re: About Question enthuware.ocajp.i.v7.2.1113 :
Posted: Sun Feb 02, 2025 10:07 am
by admin
Well, the problem statement is asking about the return type of the method. It is not talking about the type of the value or of the variable that is actually returned by the method!