#622 – Sealing vs. Not Sealing

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.

Advertisements

About Sean
Software developer in the Twin Cities area, passionate about .NET technologies. Equally passionate about my own personal projects related to family history and preservation of family stories and photos.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: