I'm currently working on incorporating a method in a superclass that should be accessible for use, but not modifiable, in subclasses. Take a look at the following:
export abstract class BaseClass {
universalBehavior(): void {
doStuff(); // Perform some standardized actions consistently across all subclasses
specializedBehavior(); // Delegate specialized actions to subclasses
}
protected abstract specializedBehavior(): void;
}
My aim is for any subclass of BaseClass to have the option to exclude the implementation of universalBehavior()
, and furthermore, not even permitted to provide an implementation. Is this capability not yet feasible in TypeScript? Intellisense gives me errors when I omit the implementation in my subclasses. The most effective approach I've found so far is as follows:
export class SubClass extends BaseClass {
universalBehavior(): void {
super.universalBehavior();
}
specializedBehavior(): void {
// Implement specific behavior for the subclass here
}
}
Evidently, this poses an issue because I must ensure no subclass ever implements universalBehavior()
with anything other than a call to super.universalBehavior()
.