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 ?
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 -
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?
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.
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 .