class
Super{
public int field = 0 ;
public int getField(){ return field;}
}
class Sub extends Super{
public int field = 1 ;
public int getField(){ return field;} public int getSuperField(){ return super .field;}
}
public class FieldAccess{ public static void main(String[] args){
Super sup = new Sub(); // Upcast
System.out.println( " sup.field= " + sup.field +
" ,sup.getField()= " + sup.getField());
Sub sub = new Sub();
System.out.println( " sup.field = " + sub.field +
" , sub.getField() = " + sub.getField() +
" , sub.getSuperField()= " + sub.getSuperField());
}
} /* Output
向上转型属性调用父类的属性,调用方法则是调用覆写的方法,用super则是调用父类方法
public int field = 0 ;
public int getField(){ return field;}
}
class Sub extends Super{
public int field = 1 ;
public int getField(){ return field;} public int getSuperField(){ return super .field;}
}
public class FieldAccess{ public static void main(String[] args){
Super sup = new Sub(); // Upcast
System.out.println( " sup.field= " + sup.field +
" ,sup.getField()= " + sup.getField());
Sub sub = new Sub();
System.out.println( " sup.field = " + sub.field +
" , sub.getField() = " + sub.getField() +
" , sub.getSuperField()= " + sub.getSuperField());
}
} /* Output
向上转型属性调用父类的属性,调用方法则是调用覆写的方法,用super则是调用父类方法