Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Mock question on polymorphism

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I found this question in a mock exam and I can't find the answer:

10. interface A { public int getValue() }
11. class B implements A {
12. public int getValue() { return 1; }
13. }
14. class C extends B {
15. // insert code here
16. }
Which three code fragments, inserted individually at line 15, make use
of polymorphism? (Choose three.)

A. public void add(C c) { c.getValue(); }
B. public void add(B b) { b.getValue(); }
C. public void add(A a) { a.getValue(); }
D. public void add(A a, B b) { a.getValue(); }
E. public void add(C c1, C c2) { c1.getValue(); }

According to me, all answers make use of polymorphism. But, we have to choose three. So which three and WHY???

Thanks in advance.
Jean
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the answer is B,C,D
because Interface A and Class B references can hold object of Class C.
 
Ranch Hand
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How about A,B and E? :roll:
 
Jean Vilart
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Ameen Khan, thank you for your answer but I don't understand it.

Dear Akhilesh Trivedi, I am as most interested in the right answer as in its explanation. Could you please elaborate why you think those are the right answers.

Thanks
 
Ameen khan
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Options A and E have Reference of type C. it means the only object of type C can be passed in method "add" not object of type B.
So if Reference and object belongs to same type there is no polymorphism.
 
Jean Vilart
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
aaaaaaaaah ok, now it makes sense !
Thanks a lot for the clear explanation.
 
Akhilesh Trivedi
Ranch Hand
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is really catchy to have getValue() inside another method add() than to have it simply redefined/overridden as it is in C. Why? let us see...



I go for

and I am done! !!
first option is out, there is no polymorphism here... only inheritance

Option B,



Any polymorphism in above case?? All is right at its place I believe.

Option C


Option D will also be counted on same basis.

Option E same as one have three C objects c1, c2 an c.add(c1,c2) in main.


I feel only C and D are ok... any suggestions? :roll:
[ August 07, 2007: Message edited by: Akhilesh Trivedi ]
 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Akhilesh
In option 2
if we do c.add(c) then also there is polymorphism
I think correct option are B,C and D
 
Akhilesh Trivedi
Ranch Hand
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Viral Gala:
hi Akhilesh
In option 2
if we do c.add(c) then also there is polymorphism
I think correct option are B,C and D



Yes viral! But the main() method part was nowhere supplied in the question, I put it to explain that there is a possibility that polymorphism can be bypassed. It was nowhere mentioned that I should use same object for calling the method and for passing as parameter.
 
All of the following truths are shameless lies. But what about this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic