• Post Reply Bookmark Topic Watch Topic
  • New Topic

Change in the flow  RSS feed

 
asha ganapathy
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When i use this code i get the output as 40


But when i use this code i get the output as 30


Can anyone please explain about why there is a change in the program flow compared to the 1st case?

Thanks in advace
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you add some println statements, you can see what's happening...

You will see that the difference is in the Base constructor. When "static" is used, the call to addValue invokes the addValue method defined in the Base class. But when "static" is not used, the call to addValue invokes the addValue method defined in the Derived class. Do you see why?
 
asha ganapathy
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Marc,

Even i did the same, by giving the messages in between.
But what i didnt understand is why should static methods behave(i.e. call the derived constructor) this way?

Thanks
Asha
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See this article on polymorphism.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!