I created two programs based on Chapter 8 Question 3. See the following programs. I noticed that shadowing the base class avgWeight is OK but the answer says it is not. Can anyone explain why?
public class GenericFruit extends Object {
protected float avgWeight = 10;
protected float caloriesPerGram;
String varietyName;
// class definition continues with methods
}
public class Apple extends GenericFruit {
private float avgWeight;
public static void main(String argv[]) {
GenericFruit f;
Apple a = new Apple();
a.avgWeight = 20;
f = a;
System.out.println(f.avgWeight );
System.out.println(a.avgWeight );
}
}
[This message has been edited by Zheng Huang (edited January 13, 2001).]