• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

constructor calling super

 
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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

why not a=10 and b=16 ?

can any one justify this ?
 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 643
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
to have more fun add below code in the main method and see the output
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how do you know that the overriden methoed is called.......
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks...that was a good explaination.
 
What's wrong? Where are you going? Stop! Read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic