Forums Register Login

constructor calling super

+Pie Number of slices to send: Send


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

why not a=10 and b=16 ?

can any one justify this ?
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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

 
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
to have more fun add below code in the main method and see the output
+Pie Number of slices to send: Send
 

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 ]
+Pie Number of slices to send: Send
how do you know that the overriden methoed is called.......
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
Thanks...that was a good explaination.
straws are for suckers. tiny ads are for attractive people.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 790 times.
Similar Threads
doubt
Q on Method Invocation !!
Which method is called????
constructor
instantiation?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 18, 2024 23:59:54.