Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

SuperClass and SubClass doubt

 
S Venkatesh
Ranch Hand
Posts: 464
Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Please see the code below




When i execute the above code, i see the following output

I am in main() method
I am in X initialization block with mask value as :: 255 and 0
I am in X Constructor
I am in mask() in Y
I am in Y initialization block with :: 65280 and 255
I am in Y constructor
I am in mask() in Y

how the mask() on Y called from X even though the Y object is not yet created?

Thanks
Venkatesh S
[ June 13, 2007: Message edited by: Venkatesh Sai ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Venkatesh Sai:

how the mask() on Y called from X even though the Y object is not yet created?


The Y object currently is in its creation process - the execution of the X constructor is part of the creation process.

In Java, you can call polymorphic methods from constructors, which can lead to some nasty effects. It can also be quite helpful. Just be very careful when you do so.
 
S Venkatesh
Ranch Hand
Posts: 464
Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ilja Preuss:


The Y object currently is in its creation process - the execution of the X constructor is part of the creation process.

In Java, you can call polymorphic methods from constructors, which can lead to some nasty effects. It can also be quite helpful. Just be very careful when you do so.


can you please eloborate on this?
 
Umashankara Kalaiah
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Object instance creation will be happened before the object initilization. they are 2 different steps happened when you call new A(). constructor is object initilization step by that time object is already created and available for initilization.

when you call new A(), jvm follows following steps

1) calculate the size of an object
2) allocation - allocating memory space with the size of an object plus the growth later, if possible to know in advance
3) binding methods
4)calling an initializing code (namely, constructor) of superclass.
5) calling an initializing code of class being created.
 
S Venkatesh
Ranch Hand
Posts: 464
Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I belive i got an answer .
Thanks for the replies.

This is how it happens.

1) calculate the size of an object - the size is mostly the same as that of the class but can vary. When the object in question is not derived from a class, but from a prototype instead, the size of an object is usually that of the internal data structure (a hash for instance) that holds its slots.

2) allocation - allocating memory space with the size of an object plus the growth later, if possible to know in advance

3) binding methods - this is usually either left to the class of the object, or is resolved at dispatch time, but nevertheless it is possible that some object models bind methods at creation time

4) calling an initializing code (namely, constructor) of superclass

5) calling an initializing code of class being created
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As a rule, I like to invoke only final methods from a constructor in order to avoid unpleasant surprises. In your example, the overridden mask() method can easily be changed (perhaps accidentally) to rely upon a variable in Y class, even though Y has not yet been fully created. I am fairly sure that the variable yMask has a value of 0 when Y's mask() is invoked, so you can see what would have happened if mask() were to be changed to use or return yMask.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic