• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

confusion in the output

 
jayalakshmi charugundla
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

The following program's output is 40 but as I know the answer would be 30. Why it's giving 40 instead of 30?
Please explain this



Thanks in advance
jaya
 
Vijitha Kumara
Bartender
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How many times the "addValue()" method being invoked upon creating a "Derived" instance ?
 
Nathan Warner
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason the value is 40 instead of 30 is because it uses the Derived class version of the addValue method in both calls so it is 20+20 which is 40 instead of the 20+10. This deals with the method being overridden in the subclass.
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


why is the output in this case 30 and not 50? isnt addValue() called two times?
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when a class is loaded all its instance variables are given their default values like int will get 0 etc....
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



study this example which will clear all your doubts.......
 
jayalakshmi charugundla
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Ankur. You have explained it very well.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic