File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Shorthand operators 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 "Shorthand operators" Watch "Shorthand operators" New topic

Shorthand operators

Arathi Rajashekar
Ranch Hand

Joined: Nov 20, 2001
Posts: 112
Why does it give answer 22

How will we know which operation it performs first

Arathi<br />Sun Certified Java Programmer
Vikrama Sanjeeva
Ranch Hand

Joined: Sep 02, 2001
Posts: 756
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.

Count the flowers of your garden, NOT the leafs which falls away!
Prepare IBM Exam 340 by joining
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1873
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
I agree. Here's the link:
subject: Shorthand operators
jQuery in Action, 3rd edition