Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 400811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T16:58:36+00:00 2026-05-12T16:58:36+00:00

class Vehicle { public int wheels; public int lights; } class Car extends Vehicle

  • 0
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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T16:58:36+00:00Added an answer on May 12, 2026 at 4:58 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following code: public class Vehicle { public void StartEngine() { // Code
I have a base class vehicle and some children classes like car, motorbike etc..
Given a Vehicle class and a VehicleProperty class... public class Vehicle { public virtual
So, if i have: public class Sedan : Car { /// ... } public
A static check tool shows a violation on the below code: class CSplitFrame :
class Foo { static bool Bar(Stream^ stream); }; class FooWrapper { bool Bar(LPCWSTR szUnicodeString)
class someclass {}; class base { int a; int *pint; someclass objsomeclass; someclass* psomeclass;
class AbstractQuery { virtual bool isCanBeExecuted()=0; public: AbstractQuery() {} virtual bool Execute()=0; }; class
class C { T a; public: C(T a): a(a) {;} }; Is it legal?
class Tag(models.Model): name = models.CharField(maxlength=100) class Blog(models.Model): name = models.CharField(maxlength=100) tags = models.ManyToManyField(Tag) Simple

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.