• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Good practice question from .VoodooExam Guess o/p

 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi friends,
this is a good one , guess the o/p. good luck,


Options :
a . 2
b . Compile error " No method matching get ( ) found in class Q026 . "
c . 5
d . NullPointerException thrown at run-time .
But the answer is none of these, can u get guess what it is
good luck
 
Ranch Hand
Posts: 330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
B's get method
6
is this correct?
 
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
6
 
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vishy,
Tha ans should be "B's get method" and 6
Here is how 6 is printed.
1)The line Q028 obj = new Q028();invokes the no-args constructor of the class B, which in turn invkes the no args constructor of A So i is incremented twice and the value is now '2'.
2) A ob =new A();This invokes the no-args constructor of A so, i is '3'.
3)Q028 invokes the getmehtod() of B. which returns (i+3) which is 6 now.
I hope I am right.
 
Vishy Karl
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guyz,
U all r right, actually missed one thing in the question, actually the output still remains the same.
Q028 obj = new Q028();
A ob = new A();
ob = (A)obj; //----------this is the change.
System.out.println(ob.get());
Now I want to confirm with u all ,the casting with (A)obj still call the get method of class B as Q028 extends B right ?? do explain
thanks,
 
dennis zined
Ranch Hand
Posts: 330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Polymorphism is in play at ob = obj. ob which is of type A is assigned a subclass of its type Q028. The get() method is overridden in B and inherited in Q028. So which get() method is called will depend on the object's type at runtime which, according to your code, is Q028.
 
God is a comedian playing for an audience that is afraid to laugh - Voltair. tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic