I often have superclasses that contain a more abstracted form of a class as a member variable than my subclasses need. I find my code littered ((ClassName)variableName).methodName()
. Is the best way to solve this to create a protected/private method called something like getVariableNameAsClass()
? It's not much shorter in letters, but an IDE can do that much faster than maneuvering around with the keys to add in parenthesis. Or is there a better solution?
The reason I need to cast is because all of the subclasses of Line
use an Output
class, so I put the getters and setters that relate to the Output
class in the base class. The issue is that an AudioLine
uses and AudioOutput
, and needs to define additional methods that use the more specific features.
So, here's my entire class design: I have a Line
class with two subclasses, VideoLine
and AudioLine
, which delegate some of their functionality to a internal Output
class. Output
class has a AudioOutput
and VideoOutput
subclass. I defined all the functionality for the Line
class that relies only on methods from the Output
class inside the Line
class, in accordance with DRY. Line
's AudioLine
and VideoLine
both deligate additional functionality to AudioOutput
and VideoOutput
. Since they both set up the Output
protected variable in their constructors (and therefor KNOW what subclass it contains), it seemed to make sense to caste it.