File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Mock question on polymorphism

 
Jean Vilart
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Ameen khan
Ranch Hand
Posts: 52
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Akhilesh Trivedi
Ranch Hand
Posts: 1599
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about A,B and E? :roll:
 
Jean Vilart
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
aaaaaaaaah ok, now it makes sense !
Thanks a lot for the clear explanation.
 
Akhilesh Trivedi
Ranch Hand
Posts: 1599
  • 0
  • Mark post as helpful
  • send pies
  • 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 ]
 
V Gala
Ranch Hand
Posts: 113
  • 0
  • Mark post as helpful
  • send pies
  • 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: 1599
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic