0

Suppose I have the following classes:

class car1 {}
class car2 {}
class car3 {}
class car4 {}

Support I also have the method: queryCar()

private Object queryCar()
{
     int version = getCarVersion(); // returns car version
     if (version == 1)
         return new car1();
     else if (version == 2)
         return new car2();
     else if (version == 3)
         return new car3();
     ...
}

I have another method, doStuff()

private void doStuff()
{
     // psudocode
     if queryCar() returns a car1, I want to create a JPanel with an instance member of type car1
}

How do I accomplish said psudocode? InstanceOf works for determining the class. However, I only want one class to autogenerate that car on runttime. (Thinking of an analog of C++ pointers in java)

3
  • I didn't understand the problem with instanceof Commented Nov 10, 2010 at 21:28
  • Yeah, did you actually try this before posting? Commented Nov 10, 2010 at 21:28
  • I want to be able to downcast the "car" variable to any type of car (car1, car2, etc) Commented Nov 10, 2010 at 21:47

3 Answers 3

5

You should use inheritance do do what you need.

abstract class Car {
    public Car queryCar();
    public int getCarVersion();
    public void doStuff() {
        JPanel j = new JPanel();
        j.add(new JLabel(queryCar().getCarVersion()));
    }
}

class Car1 extends Car {
    public Car queryCar() { return new Car1(); }
    public int getCarVersion() { return 1; }
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can do this using instanceof with Object like you did, but it might make it easier to use a car interface like this:

interface Car {}
class Car1 implements Car {}

private void doStuff {
    Car car = queryCar();
    if(car instanceof Car1) {
        Car1 theCar = ((Car1) car);
        theCar.car1OnlyMethod();
        //Or
        ((Car1) car).car1OnlyMethod();
    }
}

3 Comments

Did you mean if (car instanceof Car1)? I'm guessing that's why someone downvoted.
Yes, I typed it out too quickly and missed the 1. I fixed it. Thanks.
Can I access Car1's instance members in your doStuff method? Is it possible to downcast the variable, car, to a car1?
0

It depends on how much common behaviour you have between your carN classes. In Java, everything automatically extends Object so you always have a common base class, but Object is probably not a particularly useful common base.

Generally, put common behaviour in, say, Car and add or override version-specific behaviour in each of the subclasses.

EDIT: In your doStuff method you should consider carefully whether you really need to inspect the subclass. It will be difficult to maintain if you do require different behaviour for each subclass, so you should think about whether you can move some logic into the carN classes or the Car superclass and remove the dependency. If you can write your doStuff in terms of a more general Car interface/superclass then your code will be cleaner and easier to understand and maintain.

Comments

Your Answer

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