class Vehicle {
public int wheels;
public int lights;
}
class Car extends Vehicle {
public int wheels =4;
public int lights =2;
public void lights_on( int lights) {
//Code to onlights
}
}
class BMWCar extends Car {
public int wheels = 4;
public int lights = 4;
}
public class TestCar {
public static void main(String args[]) {
//Creating a new instance of BMWCAR by calling the default
// constructor provided by the constructor
bmwCar = new BMWCAR();
bmwCar.lights_on();
}
}
In the above example, when I run the TestCar.java file, The JVM’s class loader loads the TestCar file into the method area, and it executes the main method. When a bmwCar instance is created, it calls the default constructor of the BMWCar class and executes the super constructor, which is the Car class’s default constructor. I would like to know why when the bmwCar.lights_on(); method is called the JVM looks for the lights_on method and in the bmwCar object it initalizes the value of instance variables wheels and lights i.e. 4 and 4.
When the lights_on(); method is executed in the Car class, does the JVM reintialize the value? How is the reference passed from bmwCar.lights_on to the Car class lights_on method? I am looking for a detailed answer about the workflow.
You are declaring member variables
wheelsandlightsin classVehicle, and then you’re declaring member variables with the same name in subclassesCarandBMWCar.Note that member variables are not overridden like methods; the member variables in your subclasses hide the variables in the superclasses.
So, there is not one set of member variables – there are three! You see one of those three depending in which class your method is. The member variables of the superclasses are not somehow reinitialized. Inside the method
lights_onin classCar, you’ll see the variableswheelsandlightsof classCar, with the values 4 and 2.Using member variables with the same name as in a superclass should be avoided, as it’s confusing, and it’s not doing what you might think it does.