How the field of parent stored in object of a subclass?

The output of the following code is

1

3

My question is how the instance of Sub, namely s

, stored the value of var in class Super.

class test{

publicstaticvoid main(String[] args){

Super s=new Sub();

System.out.println(s.var);

System.out.println(s.getNumber());

}

}

class Super{

int var =1 ;

publicint getNumber(){

return var;

}

}

class Subextends Super{

int var=3;

publicint getNumber(){

return var;

}

}--

[1424 byte] By [euphoniousa] at [2007-10-2 5:39:52]
# 1

as when you cast your Sub-instance as Super, it will access the Super's field directly, but the method is an overidden one (or at least its signature hides the signature of the super-method).

fields never get overridden, methods do. fields can only be visible in a subclass, but they don't really belong to the subclass. that's the interesting thing 'bout methods ;-]

/meta

metaa at 2007-7-16 1:50:13 > top of Java-index,Java Essentials,Java Programming...