C# Lessons

Sealed modifier #L6

I suppose, this post will be the shortest of all. Today, I would like to explain to you what stands for sealed keyword.

Sealed

The explanation is very simple and intuitive – the classes that are marked with this modifier are just treated as sealed. It means that they can not be base classes for others – you can not inherit them.

It is also possible to use this modifier on the properties or methods that overrides a virtual properties or methods in a base class.
Thanks to that, we can for example derive from our class and be sure that no one will override our chosen virtual methods/properties.
Check this example to understand it better.

Good to remember:
In practice, you should never need to seal classes, as this prevents their further extension. Use the sealed modifier when there are important reasons for this – eg the class contains information that can not be disclosed in child classes for security reasons.

Leave a Reply