C#. Prohibition of inheritance. Keyword sealed. Features of use. Example

Prohibition of inheritance. Keyword sealed. Features of use. Example


Contents


Search other websites:




1. Keyword sealed. The cases of using

C# introduced the sealed keyword, which is used in two cases:

    • if you need to prohibit inheritance from a certain class. In many hierarchies, classes that are located at lower levels can be designated as sealed;
    • if you need to prohibit overriding a certain method in the class hierarchy. This situation is possible when the methods of the inherited classes in the class hierarchy redefine the virtual methods of the base classes.

 

2. Applying the sealed keyword to a class. General form. Example

The general form of using the sealed keyword for a class is as follows:

sealed class ClassName
{
  // ...
}

where

  • ClassName – the name of the class to be prohibited from inheriting.

If you try to inherit another class from the ClassName class, an error occurs at the compilation stage. In this case, the class ClassName itself can be inherited from another class.

For example, in the following code

// Class that is forbidden to inherit
sealed class A
{
  // ...
}

// A class that tries to inherit the class A
class B : A
{
  // ...
}

the compiler will generate an error

'B': cannot derive from sealed type 'A'

 

3. Applying the sealed keyword to a method. General form. Example

The sealed keyword can be applied to a method as well. Using sealed for a method means that the method is not allowed to be overridden in inherited classes. Therefore, such a method should be declared with the override keyword. And this, in turn, means that a class with a sealed method must inherit another base class that contains a method with the same name.

To apply the sealed keyword to a class method, the following conditions must be met:

  • the class must be inherited from another (base) class;
  • in the base class, a method must be declared that is redefined in this (derived) class. This base class method must also be declared with the keywords virtual or override (if the base class is also inherited).

The general form of using the sealed keyword for a method is as follows:

class ClassName : BaseClassName
{
  // ...

  access_modifier sealed override MethodName(parameters)
  {
    // ...
  }
}

where

  • ClassName – name of the class in which the sealed method with the name MethodName is declared;
  • BaseClassName – the name of the class that is the base of the ClassName class. The prohibition of overriding a method in a class provides that this class is inherited from another base class in which the method of the same name is redefined. Therefore, if you need to use the sealed keyword for some method in a class, this class must be inherited from another class;
  • access_modifier – one of the access modifiers is public, protected, internal, protected internal. The private access modifier cannot be applied to the sealed method;
  • MethodName – name of the method that is declared as sealed. This method must be declared with the override keyword.

Example.

A hierarchy of classes A, B, C is declared. In class C, a method is declared that cannot be redefined.

// Base class for classes B, C
class A
{
  // Method that is overridden in derived classes
  public virtual void Method()
  {
    Console.WriteLine("MethodA()");
  }
}

// Class that inherits class A
class B : A
{
  // This class declares a method that is not forbidden to override in derived classes.
  public override void Method()
  {
    Console.WriteLine("MethodB()");
  }
}

// Class that inherits class B
class C : B
{
  // A method that cannot be overridden in derived classes,
  // is declared - with the keyword sealed
  public override sealed void Method()
  {
    Console.WriteLine("MethodC()");
  }
}

If try to inherit from class C another class D and declare a method in class D that overrides the Method() method of class C as shown below

...

// Class D inherits class C
class D : C
{
  // Error! The method Method() cannot be overriden
  public override void Method() // ошибка!
  {
    Console.WriteLine("MethodD()");
  }
}

...

then the compiler will throw an error

D.Method(): cannot override inherited member C.Method() because it is sealed

To correct the situation, you need to perform one of two actions (depending on the task):

  • remove the word sealed before the method name in class C;
  • change the word override to the word new before the name of the method in class D.

 

4. Is it possible to declare a sealed method in a sealed class?

Yes, it is. The following example declares a sealed class and a sealed method in this class.

// Base class
class A
{
  // ...

  // virtual method
  protected virtual void Method()
  {

  }
}

// Class that inherits class A
sealed class B : A
{
  // ...

  // method that overrides the base class method of the same name
  protected sealed override void Method()
  {

  }
}

Although class B cannot be inherited, the compiler does not prohibit such code.

 


Related topics