• 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
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Overriding

 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone please help me to find the reason for the resultant output of the following code?



The output is:



I always thought the second output value would be
since the variable base points to the Child object.

Does anyone have an explanation please.
 
Ranch Hand
Posts: 55
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Methods get overridden fields get shadowed, therefore methods depend on the object while fields depend on the reference type, since you declared the reference as Base, you get the value of the base field.
 
Ranch Hand
Posts: 349
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Completely agree with Mark .
Method overriding is done at compile and run time .Compiler will resolve which method to call and further resolved at runtime to the correct version of that method to call.Here at compile time compiler will call the show() and further at runtime it decides the Child version's of show() , because the actual object of type Child .But in the case of property the reference comes into the picture and the behavior is as expected !!!

Hope you understand .

Thanks ,
Satya
 
Marshal
Posts: 75874
361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

S Majumder wrote: . . .
Method overriding is done at compile and run time . . .

No, overriding is resolved at runtime. At compile time there is a check for which methods override each other, but the actual binding is not done until runtime.
 
S Majumder
Ranch Hand
Posts: 349
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

At compile time there is a check for which methods override each other, but the actual binding is not done until runtime



I'm trying to explain this but in a different words ;)

Satya

 
I can't beleive you just said that. Now I need to calm down with this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic