• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

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.
 
Is that a spider in your hair? Here, threaten it with this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic