• 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

Doubt about instance members and polymorphism

 
Ranch Hand
Posts: 34
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

i did not understand this code in main() method

System.out.println(b.i) is not accessing -1  but accessing 99 from base class...!
What would be the reason...But it is accessing method properly...?


can somebody clear it..? please.
 
author
Posts: 23951
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

kiran madhan wrote:
System.out.println(b.i) is not accessing -1  but accessing 99 from base class...!
What would be the reason...But it is accessing method properly...?



Well, simply, polymorphism does not apply to instance variables. The variable to access is determined at compile time, and the compiler uses the reference type to choose the variable to access.

Henry
 
kiran madhan
Ranch Hand
Posts: 34
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
wow...thanks Henry Wong for your valuable answer....thanks a lot
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi kiran madhan,

First of all, a warm welcome to CodeRanch!

kiran madhan wrote:System.out.println(b.i) is not accessing -1  but accessing 99 from base class...!
What would be the reason...But it is accessing method properly...?


Here are two very simple (and hopefully easy to remember) but very, very, very important rules (which you must know by heart):
  • Which instance variables you can access is determined at compile time based on the reference variable type.
  • Which instance methods you can call/invoke is determined at compile time based on the reference variable type. Which instance method is actually executed is decided at runtime based on the type of the actual object (= polymorphism).


  • Hope it helps!
    Kind regards,
    Roel
     
    kiran madhan
    Ranch Hand
    Posts: 34
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks a lot dear sir....Roel De Nijs...!
    It is the answer cleared everything...!
     
    reply
      Bookmark Topic Watch Topic
    • New Topic