Ok, so, for example, let's say I have an abstract class called "Car." The Vehicle class, among other things, has a static variable called wheels, which is not initialized. I want other subclasses to expand from the Vehicle class, such as Motorcycle and Truck, and the wheels are initialized in these subclasses.
The code:
public abstract class Vehicle { static int wheels;
But below does not work:
public class Motorcycle extends Vehicle { wheels = 2; }
Is there any way to do this efficiently?
EDIT: Thanks to all the people who answered so far. I get that instantiating is probably a better way than putting them in separate classes, but I don't get the “static” part of java, so I need a little help here.
What I'm trying to do for my program has separate sprites for motorcycle and truck classes, and I want them to be static, so I don’t have to reload the image every time I create a motorcycle or truck instance. In addition, they will have almost the same properties with each other, so they will both extend from the superclass Vehicle.
The only other way I see this is to simply not declare the sprite variable in the Vehicle class, but in the Motorcycle / Truck class, as shown below:
public abstract class Vehicle {
user1935527
source share