aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Mock question on polymorphism Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Mock question on polymorphism" Watch "Mock question on polymorphism" New topic
Author

Mock question on polymorphism

Jean Vilart
Greenhorn

Joined: Aug 03, 2007
Posts: 6
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

Joined: Jun 10, 2007
Posts: 52
I think the answer is B,C,D
because Interface A and Class B references can hold object of Class C.


SCJP 5.0<br />Next-> I Don't Know
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1511
How about A,B and E? :roll:


Keep Smiling Always — My life is smoother when running silent. -paul
[FAQs] [Certification Guides] [The Linux Documentation Project]
Jean Vilart
Greenhorn

Joined: Aug 03, 2007
Posts: 6
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

Joined: Jun 10, 2007
Posts: 52
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

Joined: Aug 03, 2007
Posts: 6
aaaaaaaaah ok, now it makes sense !
Thanks a lot for the clear explanation.
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1511
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

Joined: Aug 06, 2007
Posts: 113
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

Joined: Jun 22, 2005
Posts: 1511
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
 
subject: Mock question on polymorphism
 
Similar Threads
Inherited Method Calls from Mock
how to decide valid polymorphic call
OOP - Inheritance Question
How to call java method into jsp file?
How to call java method into jsp file?