wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes constructor calling super 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 "constructor calling super" Watch "constructor calling super" New topic
Author

constructor calling super

saikrishna cinux
Ranch Hand

Joined: Apr 16, 2005
Posts: 689


here the o/p is b = 0 b = 16

why not a=10 and b=16 ?

can any one justify this ?


A = HARDWORK B = LUCK/FATE If C=(A+B) then C=SUCCESSFUL IN LIFE else C=FAILURE IN LIFE
SCJP 1.4
Chetan Raju
Ranch Hand

Joined: Aug 02, 2006
Posts: 109
please observe that your call method is overridden and you are creating a object of Test(). so call meth of Test will be called. Principles of overriding.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
And that is why I believe you should never call a non-private method from a constructor. When I write a base class I want to know it gets created the way I want it to, not in a way some unknown subclass wants done.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Gowher Naik
Ranch Hand

Joined: Feb 07, 2005
Posts: 643
Check code below which same code as above code this code will help to understand.

call starts from main method
Test14 constructor is called
Test14 constructor calls super
Just constructor calls super
after Just constructor super call completes
call method is called in Just constructor which calls
call method in Test14 class(not from Just class) but in Test14 class
b is still to initilize so b=0;
then contol falls back to Test14 constructor which calls
call method in Test14 class this time b=16
so op is 0 and 16
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
Initialization

In Java , when an object is created, initialization is done in this order:

.Set fields to default initial values (0, false, null)
.Call the constructor for the object (but don't execute the body of the constructor yet)
.Invoke the constructor of the superclass
.Initialize fields using initializers and initialization blocks
.Execute the body of the constructor
shilpa Reddy
Ranch Hand

Joined: Jul 26, 2006
Posts: 42
How do you know that call() of test14 class is invoked....why call() of class just not invoked...why is that b is not getting initialized at the first time....do reply..thanks
Ramamoorthy Periasamy
Ranch Hand

Joined: Feb 06, 2006
Posts: 30
to have more fun add below code in the main method and see the output
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Originally posted by shilpa Reddy:
How do you know that call() of test14 class is invoked....why call() of class just not invoked...why is that b is not getting initialized at the first time....do reply..thanks


The instance initializers and constructor of the superclass (in this case the no-arg constructor) are going to be called before the instance variables in the subclass are initialized (that is given their values by an instance initializer or subclass constructor).

In the superclass constructor there is a call to an overridden method. The
overridden method will be called, and at that point, the instance variables in the subclass have their default values, and that is why 0 is printed.
[ August 02, 2006: Message edited by: Keith Lynn ]
shilpa Reddy
Ranch Hand

Joined: Jul 26, 2006
Posts: 42
how do you know that the overriden methoed is called.......
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
This is from the Java Language Specification 12.4.2

Unlike C++, the Java programming language does not specify altered rules for method dispatch during the creation of a new class instance. If methods are invoked that are overridden in subclasses in the object being initialized, then these overriding methods are used, even before the new object is completely initialized. Thus, compiling and running the example:



produces the output:

0
3

This shows that the invocation of printThree in the constructor for class Super does not invoke the definition of printThree in class Super, but rather invokes the overriding definition of printThree in class Test. This method therefore runs before the field initializers of Test have been executed, which is why the first value output is 0, the default value to which the field three of Test is initialized. The later invocation of printThree in method main invokes the same definition of printThree, but by that point the initializer for instance variable three has been executed, and so the value 3 is printed.

[ August 03, 2006: Message edited by: Keith Lynn ]
shilpa Reddy
Ranch Hand

Joined: Jul 26, 2006
Posts: 42
Thanks...that was a good explaination.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: constructor calling super
 
Similar Threads
constructor
instantiation?
Which method is called????
Q on Method Invocation !!
doubt