• Post Reply Bookmark Topic Watch Topic
  • New Topic

Overriding  RSS feed

 
Nitin Menon
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • 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.
 
Mark King
Ranch Hand
Posts: 55
  • Likes 2
  • Mark post as helpful
  • send pies
  • 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.
 
S Majumder
Ranch Hand
Posts: 349
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • 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
  • 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

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!