C# – Modifiers – sealed Modifier – Prevent Class Inheritance

So far we have discussed about different types of modifiers available in C#. In this Article we will discuss about another interesting modifier C# provides; the sealed modifier which is useful to prevent class inheritance.

The sealed Modifier

This modifier is used to seal the classes or the class members. sealed keyword is used to apply the sealing either on the classes or on the members of the classes.

When the class is sealed; it is NOT allowed to derive any classes from it. That means, inheritance hierarchy will be closed at the sealed class. If you attempt to derive a class from the sealed class; C# compiler will throw the below Error.

Error 1 'Modifiers.DerivedClass': cannot derive from sealed type 'Modifiers.SealedClass'

sealed classes can not be the base classes.

Similarly, when the class members are sealed; the members can not be override in the derived classes. That means, the class can be inherited but NOT it’s sealed members. You will get below Error, if you attempt to override sealed members in the derived classes.

Error 1 'Modifiers.SecondLevelDerviedClass.Display()': cannot override inherited member 'Modifiers.DerivedClass.Display()'

How to seal data members?

It is simple. Use the sealed keyword in front of the data member to seal the data member to NOT to inherit into it’s derived classes. One thing you need to remember, you can use this keyword for class methods directly; but when you want to use them for class properties, those should have getter and setter methods defined.

That means, it is not allowed to use sealed keyword on class properties; whose getter and setter methods are NOT defined. Otherwise, you will see below Error:

Error 1 The modifier 'sealed' is not valid for this item

Another important thing to remember when sealing the data members; those members must be overridden in the derived classes. Otherwise, you will see below Error message:

Error 1 'Modifiers.SealedMembers.Message' cannot be sealed because it is not an override

That means, it is NOT allowed to use sealed keyword on the data members defined in the base class. You are allowed to seal the data members ONLY in the derived class and those must be overridden from the base class. Here is the right way to define a sealed data member:

sealed public override String Message { get; set; }

sealed members can not be override into the derived classes. But they can be hide in the derived classes. If you attempt to override them; you will see below Error:

Error 1 ‘Modifiers.SecondLevelDerviedClass.Display()’: cannot override inherited member ‘Modifiers.DerivedClass.Display()’ because it is sealed

How to seal data members in the base class itself?

Simple, either you must define the class as sealed class or you must give the private access specifier to the data members. private members will NOT inherit to the derived classes.

Putting altogether here is the working example:

Observe that, when you attempt to call sealed method from it’s base class; it calls base class method in the Inheritance hierarchy where the method is NOT sealed.

We will discuss more topics as we go.


C# – Modifiers – sealed Modifier – Prevent Class Inheritance

Leave a Reply

Your email address will not be published.

Scroll to top