• 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
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Superclass and subclass reference type confusion

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
I have confusion regarding the output which is: sub1 2
But i can't understand why obj.i prints value 1 rather than 2
as the actual type of obj is B at run time ( it calls obj.myInt of class B???). So it must print sub2 2.
Not getting overriding concept right.Confused what gets called subclass /super class method depending on reference type.
Below is the code




 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

supriya riya wrote:
I have confusion regarding the output which is: sub1 2
But i can't understand why obj.i prints value 1 rather than 2
as the actual type of obj is B at run time ( it calls obj.myInt of class B???). So it must print sub2 2.
Not getting overriding concept right.Confused what gets called subclass /super class method depending on reference type.



Polymorphism only applies to method calls -- and specifically, only non-static method calls where overriding is taking place. For variables (either static or instance) and for other types of methods calls (such as static methods or private methods), it is completely resolved at compile time.

Henry
 
supriya riya
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks
 
reply
    Bookmark Topic Watch Topic
  • New Topic