So I wanted to inherit from a
sealed class in csharp and got burned. There is just no way to unseal it unless you have access to the source.
Then it got me thinking "why
sealed even exists?". 4 months ago. I couldn't figure it out, despite reading many things about it, such as:
- Jon Skeet's wishes "classes were sealed by default in .NET."
- Prefer composition over inheritance?
- "You should not seal all classes (...)"
- How do you mock a Sealed class?
I've tried to digest all that since then, but it's just too much for me. Eventually, yesterday I tried it again. I've scanned over all of them again, plus few more:
- Why should a class be anything other than "abstract" or "final/sealed"?
- In over 15 years programming, first time I've heard of SOLID, out of an answer from a question already linked and I obviously didn't read it all 4 months ago
Finally, after lots of pondering, I decided to heavily edit the original question based on the new title.
The old question were too broad and subjective. It was basically asking:
- In the old title: One good reason to use sealed
- In the body: How to properly modify a sealed class? Forget about inheritance? Use composition?
But now, understanding (which I didn't yesterday) that all
sealed does is preventing inheritance, and we can and should indeed use composition over inheritance, I realized what I needed was practical examples.
I guess my question here is (and in fact have always been) exactly what Mr.Mindor suggested me in a chat: How can designing for inheritance cause extra cost?