views:

193

answers:

4
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 on to the Method Area .and It executes the main Method ,When bmwCar instance is created it calls the default Constructor of the BMWCar class and executes the Super Constructor that is Car Class default constructor . I would like to know when the bmwCar.lights_on(); method is called the JVM looks for the lights_on method and in bmwCar Object it initalizes the the value of instance variables wheels and lights ie 4 and 4 .

When lights_on(); method is executed in the Car class does the JVM reintializes the value ? How is the reference passed from bmwCar.lights_on to Car class lights_on method ? please provide detailed Answer about the workflow Thanks :)

+3  A: 

You are declaring member variables wheels and lights in class Vehicle, and then you're declaring member variables with the same name in subclasses Car and BMWCar.

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_on in class Car, you'll see the variables wheels and lights of class Car, 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.

Jesper
@Jesper Thanks for your answer , I would like to know how the JVM behaves when i redeclare the variable in an Inheritance Structure . How does the JVM identifies the lights_on method and executes the method which is in the Car class , will the reference of the parent class would be added by the compiler itself .can you share some resources if any Thanks
YetAnotherCoder
I think what you want to know is what "polymorphism" is. Have a look at this from Sun's Java Tutorials: http://java.sun.com/docs/books/tutorial/java/IandI/polymorphism.html
Jesper
+1  A: 

Don't redeclare the member variables put do it like this

public class Vehicle {
  public int wheels = -1;
  public int lights = -1;
}

class Car extends Vehicle {
  {
    wheels = 4; //set inherited member variable
    lights = 2; //set inherited member variable
  }

  public void lights_on(int lights){
    System.out.println("Parameter to lights_on: "+lights);
    System.out.println("Wheels: "+wheels);
    System.out.println("Lights: "+this.lights);
  }
}

class BMWCar extends Car {
  {
    wheels = 4;
    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
    Car car = new BMWCar();
    car.lights_on(1);
    car = new Car();
    car.lights_on(1);
  }
}
jitter
@jitter Thanks for your answer , I would like to know how the JVM behaves when i redeclare the variable , I would like to know will there be any Ambiguity during the compile time itself or will there be Run time exception for redeclaring the member variables.
YetAnotherCoder
A: 

Conceptually, the JVM refers to and looks up the field by its name, type and class. See the GETFIELD opcode in the JVM spec.

In practice, the JIT compiler (at least in Hotspot) compiles to native code which simply access memory at a given offset of the object's data (i.e. the JIT compiler pre-computes the field offset). (Sorry, I don't have a reference for this, but if you get the debug version of the JVM you can ask it to output its assembler code and see that that's what it does.)

Neil Coffey
A: 

JVM STRUCTURES IN JAVA

T.V.JYOTHSNA