• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Inheritance and variable access

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I thought the output for this program would be 10 but it is 20.
Can anyone please explain me the reason.

Thanks!
Sricharan
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi sricharan,

Originally posted by Sricharan Modali:
Hi
I thought the output for this program would be 10 but it is 20.
Can anyone please explain me the reason.

Thanks!
Sricharan


Here during the runtime, for the call: a.hi(a.i), a.hi refers to the method definition in the Trial class(a is an instance of the Trial class) and a.i refers to the variable in the class A (as a is of class A type). But in the method definition of the hi in the Trial class, you are printing the local variable i and not the argument passed. The value of the local variable i is 20 and so it prints 20.
Hope this helps
bye.
sk
 
reply
    Bookmark Topic Watch Topic
  • New Topic