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 doubt 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 "doubt" Watch "doubt" New topic


Sakthi Kani
Ranch Hand

Joined: Mar 29, 2005
Posts: 98

The output is 0 and 16.
why not a=10 ???

" Don't be afraid of pressure. Remember that pressure is what turns a lump of coal into a diamond... " <br /> <br />Thanks & Regards...<br />Sakthi<br />SCJP1.4, OCA
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026
The output should be b=0 and b=16

when u instantiate "Q05" the constructor of this class is called.
From there the super class constructor is called. when you call the
method "call" from the super class constructor it is the subclass overridden
"call" which is invoked and not the super class version of "call". This is because
you had instantiated the subclass and at runtime the subclass version of
the "call" will be invoked.

Why b = 0?

because instance variables will not be initialized with values
(here b = 16) but given default values which is 0 for int until after
super class constructor completes it execution. once it is over and it
comes to subclass constructor and makes a call to the method "call"
by now b will be initialized with 16 so the second value will be 16.

Since super class version of call is never called "a" is never printed

Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
constructors are not inherited, i guess

java amateur
vidya sagar
Ranch Hand

Joined: Mar 02, 2005
Posts: 580
HI sakthi

By executing the statement new Q05()
(Let see step by step)

step - 1 calls constructor of Q05 class
(every first line in constructor calls to its immediate base class)

step - 2 which in tends call to Just class constructor

step - 3 which in tends call to Object class constructor

step - 4 object class constructor returns

step - 5 method name call() in just class executes call() method in Q05 class
(at that time b only contains default it prints 0)
(Initialize of instance variables takes place only after base class constructor completes)
(derived class method is called because instance is of derived class)

step - 6 Just class constructor returns

step - 7 method name call() in Q05 class constructor executes call() method in Q05 class
(this time given b value is intialized to it prints 16)

Hope now UR clear
Sakthi Kani
Ranch Hand

Joined: Mar 29, 2005
Posts: 98
Thank u
i got the point now
I agree. Here's the link:
subject: doubt
It's not a secret anymore!