#622 – Sealing vs. Not Sealing
July 9, 2012 Leave a comment
There are two schools of thought when it comes to sealing a class to prevent inheritance:
- Seal every class, unless you want the class to be inherited from and you plan for inheritance
- Never seal a class, unless you specifically need to prevent other classes from inheriting from it
The people who argue for sealing classes by default argue that making inheritance work properly is tricky. So if you haven’t specifically designed a class for inheritance (e.g. making certain methods virtual), you should prevent inheritance by sealing the class.
People who argue for not sealing a class by default will argue that you never know how someone might make use of your classes and it’s best to at least give them ability to try inheriting from your class. The inheriting class can then deal with any issues that come up.