1

I want to change the object of class Car to an object of class FastCar. It is easy to see that the main method returns an error in this case. I wrote it to be easier to express my question: how can I build an object of a subclass around an object of a superclass? What would be the best way considering that the classes might not be small as in the examples below? The solution should also work for big classes, with a lot of fields.

    public class Car {
        String name;
        String label;
        Car(String name){
            this.name = name;
            label = "Car";
        }

        Car(){
            this("dafaultCarName");
        }
    }

    public class FastCar extends Car{
        String howFast;
        FastCar(){
            howFast = "veryFast";
        }
        FastCar(String name){
            super(name);
            howFast = "veryFast";
        }
    }

    public static void main(String[] args) {
            FastCar fast;
            Car car = new Car("FastCarName");
            fast = (FastCar) car;
    }

UPDATE
As @Arthur said:

public class Car {
    String name;
    String label;
    Car(String name){
        this.name = name;
        label = "Car";
    }

    Car(){
        this("dafaultCarName");
    }
}

public class FastCar extends Car{
    String howFast;
    FastCar(){
        howFast = "veryFast";
    }
    FastCar(String name){
        super(name);
        howFast = "veryFast";
    }

    FastCar(Car car){
        super(car.name);
    }
}

public static void main(String[] args) {
        FastCar fast;
        Car car = new Car("FastCarName");
        car.label = "new Label";
        fast = new FastCar(car);
        System.out.println(fast.label);
    }

The constructors from FastCar suggested by @Arthur are not good because the label is not preserved.
The output is Car but I expected it to be new Label. I want some trick to convert my "car" into a "fast car" without loosing data. Also this trick should also be efficient for larger classes.

3 Answers 3

3

There are several ways to do downcast:

  1. Add constructor FastCar(Car car) in FastCar class.
  2. Introduce method public FastCar asFastCar() in Car class.
  3. Introduce util method public static FastCar castToFastCar(Car car) anywhere.
Sign up to request clarification or add additional context in comments.

2 Comments

Also, I think it is downcast.
It's a common approach and it doesn't depend of class size. But necessity in downcast is a drawback in application design. You should avoid downcast.
1

I'm not totally sure on the best way to do this, but one way you could do it is by having a Car object as a argument for the FastCar class and then add all of the variables from there. Or take in the variables that the Car class has in set them inside the Fast Car constructor.

// FastCar class
FastCar(Car car){
    super(car.name);
}

FastCar(String name){
    super(name);
}

5 Comments

These constructors keep only the name field. I want to keep everything (including the label).
label = car.label;
put that after super(car.name)
What if there would be also a field label2? And label3? And label4? And an id? And so on. I do not think it works for bigger classes as I mentioned.
I'm not sure what a more efficient way of doing it would be, but after some research online it seams that going from a superclass to a subclass is not the best way to handle things. [stackoverflow.com/questions/8758089/…
1

When you write the line:

car = fast;

java does the uppercasting automatically, so you don't have to do it manualy.

Maybe what you want to do is something like this:

Car car = new Car();
FastCar fastCar = new FastCar();
FastCar fastCar2 = new Car();//you cant do this since Car is the Superclass of Fast car
Car car2 = new FastCar();//this is right

Now to access the methods of the FastCar class with the car2 object you have to downcast like this:

FastCar fastCar3 = (FastCar)car2;//now you can access the moethods of FastCar class with the car2 object.

In general you can not make an object of the superclass been referenced by an object of a subclass exept from the previous case.

6 Comments

I think this is downcast, isn't it?
yea sorry i was confused with the names.
Maybe I didn't express the question very well. I do not have a "fast car". I have a "car" and I want to make it a "fast car".
So you want to downcast it ?
By the way i thought you ment upcasting with this: "how can I build an object of a subclass around an object of a superclass?"
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.