• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

when static why does answer change?

 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


answer here is 30???
how is code running?
 
Sheriff
Posts: 67754
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why do you think it is wrong?
 
Ranch Hand
Posts: 59
Netbeans IDE VI Editor Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you tried running it using a debbuger? Then you'll see the constructor chaining and method overriding in action.
 
maggie karve
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

since it is Base1 b = new Derived1(); ,the method of Derived class will be called but since static methods are not overridden the Base class will be called....
first one Base1 b = new Derived1(); the derived constructor is called and it calls addvalue method ..then the method of Derived class will make the value of value=20..and then it should print the value=20.....this is what i thought....



 
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

curve karve wrote:since it is Base1 b = new Derived1(); ,the method of Derived class will be called but since static methods are not overridden the Base class will be called....
first one Base1 b = new Derived1(); the derived constructor is called and it calls addvalue method ..then the method of Derived class will make the value of value=20..and then it should print the value=20.....this is what i thought....



What makes you think the addValue() method of Derived will make value =20? value+=20 translates to value = value + 20.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When new Derived1() is called we enter the constructor. The Superclass Constructor is called first because the call to it is implicit. The value of the static variable is now 10, then the super constructor returns, and the derived classes constructor finishes running incrementing the static variable by 20 to give you 30.

A harder question would be what is the total if the add value methods are NOT static. ;)
 
maggie karve
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

hey i understood....its like when Base Class constructor is called..it gives a call to derived class constructor ...and again when derived calss constructor is called the same method is executed so..values when methods are not static will be 20+20=40;
when static methods are present since static methods are not overriden..when Base constructor is called it calls method from its class itself which prints value=10 first ..when Derived constructor is called static method of this class is executed ..then value becomes 10+20=30...
m i right???

 
Justin Fleming
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

curve karve wrote:

hey i understood....its like when Base Class constructor is called..it gives a call to derived class constructor ...and again when derived calss constructor is called the same method is executed so..values when methods are not static will be 20+20=40;
when static methods are present since static methods are not overriden..when Base constructor is called it calls method from its class itself which prints value=10 first ..when Derived constructor is called static method of this class is executed ..then value becomes 10+20=30...
m i right???



yes, if the methods were not static the answer would be 40.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic