wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inheritance question 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 "Inheritance question" Watch "Inheritance question" New topic
Author

Inheritance question

Veena Pointi
Ranch Hand

Joined: Jun 20, 2002
Posts: 442

Can anybody please explain how does above code print 40?!!!
Thanks
VEena


SCJP1.4
"Continuous effort - not strength or intelligence - is the key to unlocking our potential."
*Winston Churchill
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
Originally posted by Veena Point:

Can anybody please explain how does above code print 40?!!!
Thanks
VEena

Here is the order of exection:
1. Derived constructor is invoked at line 1
2. Derived constructor invokes the constructor of its superclass Base
3. Base constructor invokes addvalue(). However, since addvalue is overridden, it will call the method defined in the calss Derive.
4. In Derive's addvalue, 20 is added to the variable value. value has now changed from 0 to 20.
5. Upon returning from the Base constructor, the Derive constructor calls its addvalue method.
6. A value of 20 is added to the variable value again. value is now 40.
Veena Pointi
Ranch Hand

Joined: Jun 20, 2002
Posts: 442
Thanks Alton.I get it.
Vidya Sethuraman
Ranch Hand

Joined: Sep 28, 2003
Posts: 45
- References to member variables are computed at compile time using the type of reference.
- References to member methods are resolved at runtime using the type of object.


Vidya <br />(SCJP 1.4)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inheritance question