• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Shorthand operators

 
Arathi Rajashekar
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why does it give answer 22

How will we know which operation it performs first
 
Vikrama Sanjeeva
Ranch Hand
Posts: 760
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Always remember
First the Super Class is loaded
Methods are invoked by Actual Object Typ
Variable are called by Reference Typ

In ur code Object of Extension is made due to which it calls its constructor, an implicit super() causes to call Base class constructor.add(1) is called of Extension rather than Base(since objects are invoked by actual object type).It causes i=2 (since i is Zero before assingment).Control after assinging i=2 comes back to Base class constructor,finds no more statement therefor returns to Extension constructor here it finds another add(2) method,again Extension's add() is called rather than Base class.Now i gets the value of 6(Before assingment i was 2).Control comes back to the original statemnt in main where first time new Extension is called.Now bogo() is called which in turn call add(8) of Extension and finally i gets value of 22(Before assingment i was 6).And finally print is called which prints 22.
Bye.
Viki.
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi arathi,
look at this current topic..its very interesting and cofusing as well. this will help u understanding how methods are invoked while dealing with parent/child classes.
u can go here
regards
maulin
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic