Variable shadowing
suggest changeVariables are SHADOWED and methods are OVERRIDDEN. Which variable is used depends on the declared type of the variable. Which method is used depends on the actual class of the object that the variable references.
class Car {
public int gearRatio = 8;
public String accelerate() {
return "Accelerate : Car";
}
}
class SportsCar extends Car {
public int gearRatio = 9;
public String accelerate() {
return "Accelerate : SportsCar";
}
public void test() {
}
public static void main(String[] args) {
Car car = new SportsCar();
System.out.println(car.gearRatio + " " + car.accelerate());
// will print out 8 Accelerate : SportsCar
}
}
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents