File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes SuperClass and SubClass doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "SuperClass and SubClass doubt" Watch "SuperClass and SubClass doubt" New topic
Author

SuperClass and SubClass doubt

S Venkatesh
Ranch Hand

Joined: Jun 27, 2005
Posts: 464

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

Joined: Jul 11, 2001
Posts: 14112
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.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
S Venkatesh
Ranch Hand

Joined: Jun 27, 2005
Posts: 464

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

Joined: Jun 13, 2007
Posts: 2
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

Joined: Jun 27, 2005
Posts: 464

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

Joined: Sep 29, 2002
Posts: 1683
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.


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: SuperClass and SubClass doubt
 
Similar Threads
Static and Non-Static Blocks
How is a compile-time constant static vaiable causing initialization???
question about code in { }
is this program fine?
Increment example