This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Shorthand operators" Watch "Shorthand operators" New topic
Author

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.
Bye.
Viki.


Count the flowers of your garden, NOT the leafs which falls away!
Prepare IBM Exam 340 by joining http://groups.yahoo.com/group/IBM340Exam/
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
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
 
wood burning stoves
 
subject: Shorthand operators
 
Similar Threads
Another question
Constructors & methods
mughal question,need help
Khalid Mock - Constructor Evaluation
Constructor Problem