I'm reading the excerpt below from Joe Albahari's excellent "C# 9 in a Nutshell" and am trying to understand what's being described here in bold. Is anyone able to explain the alternative approach in a way that I can understand better? This seems somewhat backward to me for some reason.
Alternatives to interface reimplementation Even with explicit member implementation, interface reimplementation is problematic for a couple of reasons:
The subclass has no way to call the base class method.
The base class author might not anticipate that a method be reimplemented and might not allow for the potential consequences.
Reimplementation can be a good last resort when subclassing hasn’t been anticipated. A better option, however, is to design a base class such that reimplementation will never be required. There are two ways to achieve this:
When implicitly implementing a member, mark it virtual if appropriate.
When explicitly implementing a member, use the following pattern if you anticipate that subclasses might need to override any logic:
public class TextBox : IUndoable { void IUndoable.Undo() => Undo(); // Calls method below protected virtual void Undo() => Console.WriteLine ("TextBox.Undo"); } public class RichTextBox : TextBox { protected override void Undo() => Console.WriteLine("RichTextBox.Undo"); }
If you don’t anticipate any subclassing, you can mark the class as sealed to preempt interface reimplementation.