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

  • SEARCH
  • Home
  • 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 9068363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:10:24+00:00 2026-06-16T17:10:24+00:00

public class Car { String color; public void thisIs(){ System.out.println(Calling method from Car: the

  • 0
public class Car {

    String color;

    public void thisIs(){
        System.out.println("Calling method from Car: the color is " + color);
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }
}

public class BMW extends Car {

    public void thisIs(){
        System.out.println("Calling method from BMW: the color is " + color);
    }
    public Car toCar(){
    Car newCar = new Car();
    newCar.setColor(this.color);
    return newCar;
}

}

public class AbstractTest {

    public static void main(String args[]){
        Car aCar = new Car();
        aCar.setColor("Red");
        aCar.thisIs();

        BMW aBMW = new BMW();
        aBMW.setColor("Black");
        aBMW.thisIs();

        //Car aaCar = new Car();
        //aaCar = (Car)aBMW;
        //aaCar.thisIs();

            Car aaCar = aBMW.toCar();
    aaCar.thisIs();
    }
}

I expect the result to be:

Calling method from Car: the color is Red

Calling method from BMW: the color is Black

Calling method from Car: the color is Black

But, the result I got is:

Calling method from Car: the color is Red

Calling method from BMW: the color is Black

Calling method from BMW: the color is Black

Where am I wrong?
And how can I use the method from the super class to get the data in a subclass object?
I can write a toCar() method in BMW class to do this.
But, why casting doesn’t work?
Thanks ahead!

OK! Thank you!

I got why casting doesn’t work.

So, I add a method in BMW toCar() to get the result I want.

  • 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-06-16T17:10:26+00:00Added an answer on June 16, 2026 at 5:10 pm

    Casting the object does not change the nature of the object. It is still a BMW object; casting just tells the compiler to treat it as though it were a Car object.

    As long as we’re on the subject of inheritance: there is NO need to put either the color variable or the get/setColor methods into both the super and subclass. Putting them in the car class means they are available in any subclass; they are superfluous and a bit confusing in the subclass. I would take them out entirely.

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

Sidebar

Related Questions

I have one bean like: public class Car{ String color; List<Wheel> wheels; .... }
I have two classes: class Car<T> { public string color { get; set; }
i have a class of this kind Class Car { private $color; public function
public class Test { public static void main(String[] args) { DemoAbstractClass abstractClass = new
I have a class : public class Car { public string Name { get;
I have two classes: public class RichMan { public string ID {get;set;} List<Car> _cars=new
I have two classes Car and Drivers : public class Car { public string
public class Vehicle { public string Type public List<Color> Colors; } public class Result
Lets say i have a class [Serializable()] public class Car { public string model;
Possible Duplicate: Generic type from string value Given a scenario, Class Car: Transport ,

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.