• Post Reply Bookmark Topic Watch Topic
  • New Topic

Read the code below and explain why?  RSS feed

 
purushottam singh
Greenhorn
Posts: 10
MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Output of this code is 0. Explain Why?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically because variables aren't polymorphic. If you declare a variable in a derived class, it hides any variable of the same name in the parent class. They're separate variables with separate contents.

This code would act the way you'd expect if you removed the "int a = 5" in the derived class, and replaced it with just "a = 5" in the derived constructor.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!