Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sub class methods

 
preeti khane
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Answer says 22 according to the exam .. how is that true.. I say the answer is 21
 
Reshma Shanbhag
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
thanks ... As usual I forgot about the dynamic binding call
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic