wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Inheritance" Watch "Inheritance" New topic
Author

Inheritance

archu sweet
Ranch Hand

Joined: Mar 07, 2011
Posts: 66

I'm having an super class named Base :




Subclass named Sub:


I want to know when i invoke System.out.print(s.FOO);...The super class variable gets printed...when i invoke s.eat(); the sub class method gets invoked.......why does System.out.print(s.FOO); doesnot print subclass variable ???
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38025
    
  22
Instance methods are polymorphic; they are called according to the runtime type of the object, ie Sub.
Everything else, including static fields, is not polymorphic, and called according to the declared type of the class, ie Base, in both cases. Base b = new Sub(); means its declared type is Base and its runtime type is Sub.
archu sweet
Ranch Hand

Joined: Mar 07, 2011
Posts: 66

@Campbell Ritchie :

Thanks !!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38025
    
  22
You're welcome
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Inheritance
 
Similar Threads
Question on anonymous class
Doubt Example
Overriding of final variables Vs final method
Casting question