OOP/RTII: Abstract classes with methods using uninitialized instance variables?
These are my questions in general:
Say an abstract class defines an instant variable, but does not initialize it, and then defines a method that uses that variable.
The reason that variable is not defined is that I want all subclasses to use the same method, but with their own version of that variable.
The variable should remain the same amongst all members of a given subclass, but I don't think it should be static, because that would make ALL members of the abstract superclass have the same variable, right?
From what I've gleaned from various sources, I feel that the answer lies in learning how to use class literals, instanceof, and such, but I'm not sure right now.
In more specific details:
There is an abstract superclass called Shot. A ship in the game will shoot out Shots, but there are specialized versions of these Shots, say LaserShot and PlasmaShot. Now, each shot has its own special properties such as shotDelay(The minimum time between shots), or shotSpeed(The initial velocity of said shot).
There is a method defined in class Shot that returns a boolean based on whether or not enough time has elapsed since the last shot (using shotDelay).
What is the best way to initialize these variables? In the constructor of the subclasses?
Second, the shotSpeed is used in the constructor of the abstract superclass, Shot. Basically each Shot object is an object in space, with an x,y,mass,radius,velocityX,velocityY, etc. The Velocity of each shot is based on the velocity of the ship that is shooting it, PLUS the shot's shotSpeed. However, the catch-22 that I can't figure out (elegantly, at least), comes here. Since LaserShot (or PlasmaShot) defines shotSpeed, initializing either requires a call to the superclass constructor first. But since the shotSpeed is not defined in the superclass, it can't construct, because it needs to construct the subclass to define it! Is this the flaw of poor design, or is there a tool I am not using? This is not the first issue of its kind I have encountered, but I have gotten tired of odd loop-arounds using initialization blocks and such.