Often in my designs I define an abstract superclass whose subclasses will vary mostly in their values for the fields defined in the superclass.
For example, in a game I'm developing there's an abstract class Missile
with a number of subclasses. Each one defines a different value for member variables such as MAX_SPEED
, MASS
, FLYING_FORCE
, which are then used in calculations performed in the superclass.
I use abstract methods in Missile
to force it's subclasses to define these variables. For example I simply have an abstract method defineMass()
that forces subclasses to define their own value of mass.
A different example would be my Entity
class (the superclass of all game-play classes) defining an abstract loadImage()
method that forces subclasses to define an image for themselves.
Is this good practice? Is it common?