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.
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.
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
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
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
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 ]
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 ]