• 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:

Inheritance

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Base{
int i=100;
void show(){
}
}
class Derived extends Base{
int i=10;
void show(){
}
When i say
Base b=new Derived();
b.show();
System.out.println(b.i);
b.show(); calls the method of the Derived class
that's ok.But why b.i is bringing Base variable value.
& not Derived one.
When we say Base b then b refers to class Base & when
we say b=new Derived(); then b points to Derived object.Please explain this in terms of memory allocation.
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since the variable "i" is in both classes and you put a Derived class into a Base variable type, java resolves the reference to i to be the i from the base. If, in main(), you had created the variable to be of type Derived, then "i" would resolve to Derived's value.
Think of it this way: if in Derived there was a variable "j", and you said:

Base b = new Derived();
b.j = 7;


The compile would fail because "j" is not a member of Base. Since you've created a variable of type Base, every variable and function reference must be valid for any Base variable.
Bruce.
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The thing to understand is the timing of the creation of each thing.
When b is declared (when you say Base b; )the variables for Base are created in memory. Not to confuse the object variable b (the "holder") with the internal variables that b has which may be either primitive or object variables themselves.
When an object variable is instantiated (when you say b = new Derived(); ) the Derived object is created on the heap and the reference to it placed in b. Methods are resolved at runtime, so the methods that b uses will be the Derived object methods, however variables are resolved at compile time, so the variables that b uses are the variables of Base.
As long as your Derived object is in a Base variable it will only be able to reference the instance variables of Base. That is why programmers make the effort to cast them back to their proper class at times.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic