This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

calling instance methods

 
K Anshul
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cannot call instance methods until after super constructor runs.

Then why does this code give the output 0,4
 
arnel nicolas
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the point where you create object (a), the constructor for class A
invoked the print() method.We should now that methods are resolve at runtime and instance variable during compilation,
so the print() method of class B is invoked accsessing variable i, and at that time, variable i
was not yet initialized so the default value was 0 (it is an instance variable).

After object (a) was created, its print() method was invoked (because methods are resolve at runtime)
thus the value was rounded and prints 4.

arnel
 
K Anshul
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by arnel nicolas:
From the point where you create object (a), the constructor for class A
invoked the print() method.We should now that methods are resolve at runtime and instance variable during compilation,
so the print() method of class B is invoked accsessing variable i, and at that time, variable i
was not yet initialized so the default value was 0 (it is an instance variable).

After object (a) was created, its print() method was invoked (because methods are resolve at runtime)
thus the value was rounded and prints 4.

arnel


Thanks arnel
What i wanted to know was why is the print method called when super constructor has not completed.
Page 316 K&B
You cannot make a call to n instance method or access an instance variable until after the super consructor has run


But in this case when B's print method is called from within A's constructor, super constructor of B that is A has not yet compeleted. Why doesn't the above statement apply here?
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the statement read be "should not" rather than "cannot". It's clearly possible and it works as you can see by running the example.

You "should not" do it because it makes the construction of the super class rather unpredictable. When you write such a base class and write a constructor for it you should restrict the member methods that it calls to being private. In that way you prevent subclasses overriding the method and producing unpredictable (for you, the base class provider) results.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic