File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Constructors and Polymorphism Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Constructors and Polymorphism" Watch "Constructors and Polymorphism" New topic
Author

Constructors and Polymorphism

Rashmi Liyan
Greenhorn

Joined: Aug 16, 2009
Posts: 9
Hi Guys,
I have a question about the output of below code.


Output comes as

Shape() before draw()
RoundShape.draw(), radius = 0
Shape() after draw()
RoundShape.RoundShape(), radius = 5

How do you get the output line 2 (RoundShape.draw(), radius = 0).
First code runs super constructor which is 'Shape'. Inside it draw method ( #1)
is called, but it is abstract when Shape is created. This is confusing.
Can someon explain this ?

/Rashmi
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
First the super constructor runs. At that time, the Subclass has not been initialized, so radius is 0. Once the super constructor has finished work, the Subclass constructor runs and the property fields are being initialized.

That is also why you should never call any method that takes part in inheritance from a constructor.


JDBCSupport - An easy to use, light-weight JDBC framework -
S Ali
Ranch Hand

Joined: Aug 23, 2009
Posts: 129
Then howcome it called the overriden version of draw() when the class hasn't been loaded yet?


SCJP 6
Rashmi Liyan
Greenhorn

Joined: Aug 16, 2009
Posts: 9
Hi Sebastian,
thanks for your explanation.
Yes I understand that radius gets zero since subclass is not intialised when super constructor runs.
question here is when Shape super constructor runs does it see the 'draw' method implementation which is in class RoundShape. How could you explain that?

/Rashmi
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
S Ali wrote:Then howcome it called the overriden version of draw() when the class hasn't been loaded yet?


The class has been loaded. Remember, the subclasses constructor has to call the super constructor before it does anything else.

So, the super constructor calls draw, the Subclasses property fields have the default values and hence the 0.
Rashmi Liyan
Greenhorn

Joined: Aug 16, 2009
Posts: 9
Hi Sebastian.
Does it means that Draw method in super class 'Shape' can see the
Draw method implementation in Roundshape class (Since Roundshape is also loaded ) when the Shape constructor runs?.
Which means when a Super class constructor runs , sub class's constructor is also loaded and super class constructor has access to sub class's members. Is it correct , if I say like that.

/Rashmi
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
Otherwise the compiler would choke if you tried to invoke an abstract method from a constructor.
S Ali
Ranch Hand

Joined: Aug 23, 2009
Posts: 129
I guess instance variables of Subclass got initialized after the call to super() returned .
By the way problem is solved if you add static modifier to radius .
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
No it won't. Well, it will solve your current problem since static variables are initialized upon class loading but it will bring up another.

Try to run this piece of code with your former instance variable declared as static:



The result is not what you want!
S Ali
Ranch Hand

Joined: Aug 23, 2009
Posts: 129
Oh yes variable becomes shared so it can't be used for multiple instances :S
thanks Sebastian
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Constructors and Polymorphism
 
Similar Threads
polymorphic method in abstract constructor
abstract class
which draw() is called?
cannot resolve symbol
Storing objects polymorphically in a Figure array