• 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

Overriding and Overloading

 
Greenhorn
Posts: 22
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

The following program produces 0.0 and 9.0 as output.

I don't understand why 0.0 is displayed.

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you create a new object of type MySub, first the super class constructor is called before executing any function of the sub class.
And in the super class, you are calling the overridden method disp() in the sub class. At that time only the memory is allocated for i but the value is not assigned yet as the function Math.ceil(8.4f) hasn't executed yet.
Remember that functions in the sub class will be executed only after the super class constructor has finished executing .
 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joseph Arnold wrote:Remember that functions in the sub class will be executed only after the super class constructor has finished executing .


Not necessarily. e.g. in above example, sub class method is being invoked in the super class constructor (of course, sub class method is being invoked because sub class' object is being constructed).

Joseph Arnold wrote:At that time only the memory is allocated for i but the value is not assigned yet


No. Its not like that. If no value is assigned, why would we get 0.0 as output every time? it should be some garbage value, right?

During object creation, things happen in below order (I'm not listing static init blocks, since its irrelevant for this example):
1) All class variables are assigned to their respective default values (boolean to false, int to 0 and so on; here, value of i is 0.0)
2) Super class' constructor is called
3) After completion of super class' constructor, and before continuing with sub class' constructor, variables are initialized with actual values (here, value of i is 9.0, but we've already printed it, which was 0.0 at that time)
4) Sub class constructor continues.

Now, in given example, sub class' disp method is being invoked via super class' constructor, at which time, i is assigned to its default value (which is 0.0 for double) and hence we get first output as 0.0.

I hope this helps.
 
Joseph Arnold
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually what I meant was that the value of i will not be assigned the value returned by the function 'Math.ceil(8.4f);' till the super class constructor has finished executing. Sorry, for putting it across the improperly.
Anyway thanks for explaining object creation process clearly.
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anayonkar Shivalkar wrote
3) After completion of super class' constructor, and before continuing with sub class' constructor, variables are initialized with actual values



Is it always that the variables are initialized in this way ? I mean before sub class constructor executes , are they initialized with the given values ?
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ayush raj wrote:I mean before sub class constructor executes , are they initialized with the given values ?


Yes. You can check this by calling disp method in sub class' constructor. You'll get actual initialized value (9.0). And one more thing, this double initialization (first with default value, second with actual value) also works with final variables.

ayush raj wrote:Is it always that the variables are initialized in this way ?


Not for static variables. Those are initialized by actual provided value (or default value if no value is provided) at the time of loading of that class. Thus, static variables do exist and get those values no matter when (or if) the constructor is called.
 
Ranch Hand
Posts: 144
MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good Example Pinki Roy.
 
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
[quote=Anayonkar Shivalkar During object creation, things happen in below order (I'm not listing static init blocks, since its irrelevant for this example):
1) All class variables are assigned to their respective default values (boolean to false, int to 0 and so on; here, value of i is 0.0)
2) Super class' constructor is called
3) After completion of super class' constructor, and before continuing with sub class' constructor, variables are initialized with actual values (here, value of i is 9.0, but we've already printed it, which was 0.0 at that time)
4) Sub class constructor continues.


I think step 1 and step 2 are in a reverse order. I think after a super class contructor is called and finishes, the class variable of the sub class are initialized to default values or their specific values.
Correct me if I am wrong.
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Helen Ma wrote:I think step 1 and step 2 are in a reverse order. I think after a super class contructor is called and finishes, the class variable of the sub class are initialized to default values or their specific values.


No. We can check it in above example itself.

In given code, even before super class' constructor is called, sub class' variables are initialized with default value. That is why when sub class' disp method is called via super class' constructor, 0.0 is printed. Please note that super class' constructor is not finished yet, and still we can see default value 0.0 is assigned to i.

Once super class' constructor is finished, and before sub class' constructor continues, actual value 9.0 is assigned to i. This can be confirmed by inserting a default constructor which prints value of i in sub class. There, value 9.0 will be printed.

Thus, as already mentioned, a non-static variable is initialized two times - first with default value, and second (optional) with actual value.

I hope this helps.
 
ayush raj
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good explanation by Anayonkar Shivalkar !!
 
Ranch Hand
Posts: 49
Eclipse IDE Spring Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am still confused with this kind of question.

I am not sure exactly about the order its executed and initialized , making me more confused.

When line 11 is reach, new is there so parent class constructor is called , right? , but that system.out.println isn't printed because...due overrided method?. There i still keeps default vaule and, after is when parent class constuctor finish and it starts the call to sub class constructor?

X_x

Sorry for insist on the topic ;P
 
Helen Ma
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pinki Roy wrote:




You can make a subsitution here:
MySuper obj = new MySub();
1 . When mySub is called, it calls the super().
2 . Replace super() by disp().
3 . But disp() is called from MySub's default constructor. Therefore, the overriding disp() method is called.

You can modify this code into:


MySub inherits go() from MySuper. When it calls go(), which print method it will call ? The overridng one, MySub's print()!
 
Helen Ma
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Anayonkar,

Anayonkar Shivalkar In given code, even before super class' constructor is called, sub class' variables are initialized with [i wrote:default [/i]value.



When MySub is instianted as new MySub(); what under neath the hood is that super(); is implicitly called and super() is always the first method to be called.
The default contructor of MySub class is:


Therefore, I think the super class's contructors are called before the sub classes's variables are initialized with default values or specific values.



What do you guys think?

 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Helen Ma wrote:Therefore, I think the super class's contructors are called before the sub classes's variables are initialized with default values or specific values.


Well, Helen, I don't have any documented references for it, but logically speaking, if what you say is true, then when we reach super class' constructor, sub class' variable wouldn't have been initialized to 0.0

Sub class' instance variable has value 0.0, when printed before sub class' constructor get executed. So, there are only two possible explanations:
1) Default value was assigned to 'i' before super class' constructor is called
2) Default value was assigned to 'i' during super class' constructor's execution

Now, again, I don't have any documentation, but for me, case 2 seems to be very unlikely (why super class' constructor will assign default values to sub class' instance variables?). Further to this, the moment we are back to sub class' constructor, actual value 9.0 is assigned to 'i'.

So, I still think that default values are assigned to variables before super() is invoked.

I hope this helps.
 
Helen Ma
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, thanks for your reply. It is actually a potential exam questions.
Here is the sequence I can think of:
1. new MySub () is called.
2. super() is called and MySuper() is called.
3. MySuper() calls the Object's default constructor. Object() completes.
4. MySuper() constructor has not finished, and call MySub's disp() method. At this moment, MySub is not full instantiated yet, and i is assigned to default value, which is 0.0.
5. MySuper() is completed.
6. The instance variable i is assigned to 9.0
7. MySub() is completed.




The very strange thing to me is that MySub object is full instantiated in step 7, but its disp() can be called in step 4.
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Helen Ma wrote:The very strange thing to me is that MySub object is full instantiated in step 7, but its disp() can be called in step 4.


Well, when a method 'm()' is invoked without any object reference, it is invoked like 'this.m()'. Now in given example, super class' constructor was called because we are creating object of sub class (we are not manually creating object of super class by calling 'new'). Hence, 'this' refers to object of sub class. As we know, disp() is invoked as this.disp() and 'this' refers to an object (which is not fully constructed yet) of sub class.

This can be confirmed by printing this.getClass().toString() in super class' constructor.

Helen, about your 7-step scenario, it seems probable . Only way to confirm what exactly happens is to access variable i before super() is invoked. I doubt if that is possible
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By the way, I also don't know exactly how is the sequence of object creation, but it seems that memory allocation takes place before constructor (or inside constructor, but before super()). At least that's the only justification why method of sub class can be invoked even before subclass' constructor is executed completely.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic