• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Constructors and Polymorphism

 
Rashmi Liyan
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
S Ali
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then howcome it called the overriden version of draw() when the class hasn't been loaded yet?
 
Rashmi Liyan
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Otherwise the compiler would choke if you tried to invoke an abstract method from a constructor.
 
S Ali
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh yes variable becomes shared so it can't be used for multiple instances :S
thanks Sebastian
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic