Variable shadowing

suggest change

Variables 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
    }
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents