Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inheritance question

 
Veena Pointi
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Can anybody please explain how does above code print 40?!!!
Thanks
VEena
 
Alton Hernandez
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Alton.I get it.
 
Vidya Sethuraman
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
- 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic