• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Ron McLeod
  • Tim Cooke
Sheriffs:
  • Devaka Cooray
  • paul wheaton
  • Mark Herschberg
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
  • Jj Roberts
Bartenders:
  • Carey Brown
  • salvin francis
  • Piet Souris

Polymorphism Problem

 
Ranch Hand
Posts: 634
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

iRix Technolohies mock questions



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

---------------------------------------------

Problem 2


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


both the method match,so complier erroe
 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
variable cannot be overriden.
answer will be 10
 
Ranch Hand
Posts: 276
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For your second problem, there will be no compile error. It is always acceptable to assign a class's instance to its super class's reference. So, D will be the answer.
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
as explained by Vinoth Kumar Kannan
above is the case of overloaded method,so answer is D....
 
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well
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

 
Ranch Hand
Posts: 623
1
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Problem 1:

I think polymorphism only applies to methods & not to variables of any kind .

Problem 2:

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.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic