I was wondering about the use cases of inheritance. Specifically, I have a situation in C# where I have an Axis class where you can give the class a string name, among other properties. So you can write (new Axis(“X”)) to create an Axis named X.
Because usually you’ll want to create X, Y and Z axes, I feel inclined to create three subclasses of Axis named X, Y and Z, each of which specifies in the constructor what the Axis name is.
At present there is not a lot of code in the Axis class, so code reuse is pretty minimal (though of course that could change). And to be fair, it’s also not that much trouble to write (new Axis(“X”)) instead of (new X()).
Anyway, what I’m wondering about is whether there are any disadvantages to writing my code like this, or maybe if it’s just not common practice to do it like this. Much obliged!
Imo it depends on what you're going to do with this value:
string
constructor parameter is perfectly fine here, even if you decide to always name the x-axis "X"."X"
-axis, you might want to use an enum instead, ie AxisType.X
.I wouldn't create a subclass unless each subclass has distinct behavior from the others.