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

Sub class methods

 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Answer says 22 according to the exam .. how is that true.. I say the answer is 21
 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Preeti,

1.
At //Line 1 when u create an instance of Extension, the super class 0 argument constructor is invoked which invokes the add() method with value 1, because of dynamic binding the add() method of Extension is called and not the add() method of Base.

i=0;
i+= 1*2 ;
Therefore the value of i=2;
2.
The constructor of the Child class has an explicit call to add method with value 2. Hence the value of i becomes 6.
i=2;
i += 2*2;
Therefore value of i = 6;
3.
From the method bogo() the add() method is called with a value of 8.

i=6;
i += 8*2;
Therefore value of i = 22;
when called the 'b.print()' method, it calls the method print() inherited by child 'Extension' and hence prints the value of 22.
Hope this clears your doubt
reshma
[ April 02, 2003: Message edited by: Reshma Pai ]
 
preeti khane
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks ... As usual I forgot about the dynamic binding call
 
It's fun to be me, and still legal in 9 states! Wanna see my tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic