Java -- Initializing superclass variables in subclasses?

33,163

Solution 1

If 'wheels' is static, there is only one and it will apply to all vehicles at the same time. So tricycle, a motorcycle, an 18-wheeler truck and a Ford will all have the same number of wheels.

That doesn't make sense to me. It would be better to have 'wheels' be an instance variable that is in the parent class but each subclass sets appropriately.

But you can try

Vehicle.wheels = 2;

NOTE: I'm adding to my answer since you added to your question.

I like your idea of having statics in each of the subclasses. But you should make them private. Then put an abstract method in the parent class (Vehicle) like

public abstract BufferedImage getSprite();

Then each direct subclass has to have the same method and it can return the private static variable.

Make the variable static so you only have to load them once. Make them private so that code outside the class itself can't fool with it and introduce bugs. You could make them 'final' if possible so the code in the class itself can't change it after the fact and introduce bugs. (A 'final' variable can't have its value changed but the contents of its value can change. So 'final' isn't a wonderful as it could be.)

Solution 2

What you're trying to do is fundamentally flawed. You could make Motorcycle initialize wheels once:

// Static initializer
static
{
    wheels = 2;
}

... or each time an instance was created:

// Instance initializer
{
    wheels = 2;
}

But there's just one variable - not one for Motorcycle, one for Truck etc. If you did the same thing for both Truck and Motorcycle, then whichever is initialized last would "win".

It's not clear how you want to use this field anyway - but if you just have a single static field, then that's just going to have a single value - not one per subclass.

Solution 3

Static members are only defined once and are common to every extending class. Changing the value in one of them will affect all of the others. This is what I believe you really want to achieve:

public abstract class Vehicle {
    private int _wheels; //number of wheels on the vehicle
    public int getWheels(){return _wheels;}

    protected Vehicle(int wheels){
        _wheels = wheels;
    }
}

public class Motorcycle extends Vehicle {
    public Motorcycle(){
        super(2);
    }
}

public class Car extends Vehicle {
    public Car(){
        super(4);
    }
}

Solution 4

I think there's a significantly more elegant way to do this

What I am about to propose still suffers from the limitation that you need an instance. I don't see any way around that because you want wheels to be exposed as part of the superclass, but the value of wheels is dependent on the subclass and inside of Vehicle there is no notion of a subclass type without an instance.

In my opinion, 'wheels'in this case is neither a static or non-static property. It is class metadata. And the Java way to specify class metadata is via annotations.

What you need is a user-defined annotation like this:

@Documented
@Retention(RetentionPolicy.RUNTIME)
public @interface VehicleMetadata{
    int wheels();
}

You then annotate Motorcyle as follows:

@VehicleMetadata(2)
public class Motorcycle extends Vehicle {}

In the superclass you provide an accessor that gets the value of the annotation property. I would recommend you use a "lazy evaluation" approach so you don't use reflection every time you need the value.

Note the use of this to get the instance:

private String wheelsValue;

public String getWheels() {
    if (this.wheelsValue== null) {

        VehicleMetadatane = null;
        for (Annotation annotation : this.getClass().getAnnotations()) {
            if (annotation instanceof VehicleMetadata) {
                ne = (VehicleMetadata) annotation;
                break;
            }
        }

        wheelsValue = ne.wheels();
    }
    return wheelsValue ;
}

In my opinion, this is the most elegant solution.

Solution 5

The original class declaration:

public abstract class Vehicle {
    static int wheels; //number of wheels on the vehicle
}

public class Motorcycle extends Vehicle{...}
public class Truck extends Vehicle{...}

does not work because the static variable goes with the class it was declared in. Static class variables create memory storage for only one instance of the variable per class and not per class object. When the compiler (jvm) sees the static variable in the class Vehicle it allocates memory to that variable and that memory location is static (does not change). Each subsequent use of the Vehicle class whether it is extended or instantiated as an object will point to the same location in memory for the static variable.

In order to use the static variable in the child classes you have to use it inside a method. So, you could in essence re-write your Motorcycle class like this:

class Motorcycle extends Vehicle{
    public Motorcycle(){
        wheels = 2;
    }
}

and it will compile; however, you will may not get the results you expect. For example if you do this in your code (assuming Truck class is declared like Motorcycle class and assigns 4 to wheels and there is a getter method to return the value of wheels).

Motorcycle cycle = new Motorcycle();
Truck pickup = new Truck();
...
System.out.println("Motorcycle has " + cycle.getWheels() + " wheels.");

will print:

Motorcycle has 4 wheels.

Share:
33,163
user1935527
Author by

user1935527

Updated on June 30, 2020

Comments

  • user1935527
    user1935527 almost 4 years

    Okay, so, for example, let's say I have an abstract class called "Vehicle". The Vehicle class, has, among other things, a static variable called wheels, which is not initialized. What I want to do is have other subclasses extending from the Vehicle class, like "Motorcycle", and "Truck", and in these subclasses, have the wheels initialized.

    Code:

    public abstract class Vehicle {
        static int wheels; //number of wheels on the vehicle
    }
    

    But the below doesn't work:

    public class Motorcycle extends Vehicle {
        wheels = 2;
    }
    

    Is there a way to do this effectively?

    EDIT: Thank you to all the people who replied so far. I get that making instances is probably a better way to go than to put them all in separate classes, but I don't get the "static" part of java perfectly, so I need a little help here.

    What I'm trying to do for my program is have separate sprites for the Motorcycle and Truck classes, and I want them to be static so that I won't have to reload the image every time I create an instance of a Motorcycle or Truck. Other than that, though, they'll have almost identical properties to each other, which is why they'll both be extending from the Vehicle superclass.

    The only other way I can see this being done is by just not declaring the sprite variable at the Vehicle class, but at the Motorcycle/Truck class, like below:

    public abstract class Vehicle {
    //Other coding
    }
    
    public class Motorcycle extends Vehicle {
    static BufferedImage sprite = //initialize image
    //Other coding
    }
    
    public class Truck extends Vehicle {
    static BufferedImage sprite = //initialize image
    //Other coding
    }
    
  • Lee Meador
    Lee Meador over 11 years
    All motorcycles probably will have the same number of wheels. This suggests that putting it in an argument to the constructor isn't the right place.
  • user1935527
    user1935527 over 11 years
    Wow, thank you! This was probably the most helpful comment for me, and it's exactly what I needed.