GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes confusion in the output Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "confusion in the output" Watch "confusion in the output" New topic
Author

confusion in the output

jayalakshmi charugundla
Ranch Hand

Joined: Jul 22, 2009
Posts: 57
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

Joined: Mar 24, 2008
Posts: 3833

How many times the "addValue()" method being invoked upon creating a "Derived" instance ?


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Nathan Warner
Greenhorn

Joined: Nov 16, 2009
Posts: 4
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

Joined: Sep 06, 2009
Posts: 531


why is the output in this case 30 and not 50? isnt addValue() called two times?
Ankur kothari
Ranch Hand

Joined: Sep 06, 2009
Posts: 531
when a class is loaded all its instance variables are given their default values like int will get 0 etc....
Ankur kothari
Ranch Hand

Joined: Sep 06, 2009
Posts: 531



study this example which will clear all your doubts.......
jayalakshmi charugundla
Ranch Hand

Joined: Jul 22, 2009
Posts: 57
Thank you Ankur. You have explained it very well.
 
GeeCON Prague 2014
 
subject: confusion in the output