In Java, is it possible to override member data in a subclass and have that overridden version be the data used in a super class\'s implementation?
In other words, here\
If you want to print the array of the String Stuff defined in derived class then you need to override the method readStuff in the class HardStuff by redefining the method in the class HardStuff. As the method readStuff was defined in the abstract class BasicStuff, hence it would only print the members of the class BasicStuff. Hence, add the same method in the derieved class too. Below you can find the complete code..
class BasicStuff {
protected String[] stuff = { "Pizza", "Shoes" };
public void readStuff() {
for(String p : stuff) {
System.out.println(p);
}
}
}
public class HardStuff extends BasicStuff {
protected String[] stuff =
{ "Harmonica",
"Saxophone",
"Particle Accelerator"
};
public void readStuff() {
for(String p : stuff) {
System.out.println(p);
}
}
public static void main(String []arg)
{
HardStuff sf = new HardStuff();
sf.readStuff();
}
}