Problem 1 Given the following class declarations in their respective files What is the result of compiling & running class Circle ?
correct answer: Prints 10 to the console.
wrong answer:Prints 20 to the console.
shouldn't answer be 20 since 20 would override 10
Given the following, what is the result of compiling & running class Test ?
options: A.Compiler error, reference to render is ambiguous.
B.AmbiguousReferenceException thrown at runtime.
C.Prints "render(Shape s, Circle c)" to the console.
D.Prints "render(Circle c, Shape s)" to the console.
correct answer is D.
i think answer should be A
as we are passing
new Circle,new Circle to render(c1, s1);method because
in case of overloaded methods the method with the most specific arguments are called
if the match is found such that the method argument types exactly match the types we declare in overloaded method
then that method is selected
in our case
we have two methods
SCJP 6 [86%], OCPWCD [84%], OCEJPAD [83%]
If you find any post useful, click the "plus one" sign on the right
Also remember (your second problem) that you are facing overloaded methods. If so, the method signature is "pinned" down at the compile time - compiler looks at reference variables type (not the object type) to know which method to invoke, so you've got (Circle, Shape).
At the compile time which overloaded method will be called is determined by the compiler according to the reference types. And the compiler try to match with more appropriate method. In your case the render(Circle, Shape) method exactly match with your passing reference types. Moreover, if there is no render(Circle, Shape) method in your class then it will be a compile time error since supertype reference can not assigned into the subtype reference.
I think polymorphismonly applies to methods & not to variables of any kind .
Method signatures are dependent on the order of the arguments.
As it's an example of overloading not overriding, The actual type of the object wont matter.
The method will be chosen on the basis of the type of reference variable.
Tongue wrestling. It's not what you think. And here, take this tiny ad. You'll need it.