DEV Community

Cover image for Sealed: Preventing Inheritance in C#
Hamid Molareza
Hamid Molareza

Posted on

Sealed: Preventing Inheritance in C#

By the end of this article, you can expect to:

  • Remember and Understand: Get a clear idea of what the "sealed" keyword is and what it does in C#.

  • Know Where and How to Use It: Learn the practical aspects of when and how to use this "sealed" keyword in your C# code.

In the world of object-oriented programming, inheritance is a powerful technique that allows you to create new classes based on existing ones. This can be incredibly useful for code reuse and modularity, but it also introduces the potential for unexpected behavior if not handled carefully. To address this, C# provides the sealed keyword, which serves as a protective barrier, preventing inheritance from certain classes or class members.

Defining "Sealed"

The "sealed" keyword is a modifier that restricts the ability of other classes to inherit from a specified class. Once a class is declared as sealed, it becomes a final class, unable to be extended any further. This ensures that the class's structure and behavior remain intact, preventing potential inconsistencies or unexpected changes.

Why Use "Sealed"

There are several reasons why you might want to use sealed classes:

  • Encapsulation and Code Protection: Sealing a class can help to protect its internal implementation and prevent unauthorized modifications. This can be particularly important for classes that handle sensitive data or perform critical operations.

  • Security Considerations: Sealing classes can be a valuable tool in security-critical applications. By preventing inheritance, you can reduce the risk of unexpected behavior or malicious code injection.

  • Controlling Class Structure: Sealing can be used to define a class hierarchy that is more rigid and predictable. This can be useful for ensuring that classes are used in the intended manner and that their behavior is consistent.

When to Use Sealed Classes

Sealed classes are typically used when you want to create a class that should not be modified or extended by other classes. This could include:

  • Utility Classes: Classes that provide essential functionality or utilities are often sealed to prevent their behavior from being inadvertently altered.

  • Base Classes with Fixed Behavior: Classes that define a core set of functionality and should not be customized are often sealed.

  • Performance-Critical Classes: Sealing can improve performance by reducing the overhead of dynamic dispatch, especially in cases where a class is frequently instantiated or used.

The performance benefits of sealed classes are often minor and depend on specific code patterns and compiler decisions. (See this link for more information)

  • Core System Components: Classes that form the foundation of a software application, such as fundamental data types or operating system interfaces, should be sealed to prevent unexpected or incompatible behavior from derived classes.

  • Custom Data Structures: Custom data structures, such as linked lists or trees, should be sealed to prevent their underlying implementation from being altered or misused by derived classes.

Example of 'Sealed' Class

Consider a class representing a Shape with properties like color and area. This abstract class can have sealed derived classes for specific shapes like Circle, Rectangle, and Triangle. Each derived class can provide its unique implementation of calculating the area.

abstract class Shape {
    public abstract double CalculateArea();
}

sealed class Circle(double radius) : Shape {
    public override double CalculateArea() {
        return Math.PI * radius * radius;
    }
}

sealed class Rectangle(double width, double height) : Shape {
    public override double CalculateArea() {
        return width * height;
    }
}
Enter fullscreen mode Exit fullscreen mode

Primary Constructor is used in this code. Are you not familiar? Read this article.

Conclusion

The "sealed" keyword is a valuable tool for controlling class inheritance and ensuring code integrity in C#. By understanding its purpose and using it judiciously, you can create more robust and maintainable object-oriented applications.

AI has also been used to collect and rewrite this content.

Top comments (4)

Collapse
 
webjose profile image
Info Comment hidden by post author - thread only accessible via permalink
José Pablo Ramírez Vargas
Collapse
 
hamidmolareza profile image
Hamid Molareza

Hello and thank you for your useful suggestion.
This topic (sealed) is one of the basic topics of programming and is written for the purpose of getting to know beginners.
As the topics become more specialized, I try to explain more, give more real examples and link to sources. But in my opinion, this article should remain brief and concise.
Thanks again.

Collapse
 
webjose profile image
José Pablo Ramírez Vargas

So... you didn't even open the link.

Tag your AI-assisted creations as per the guidelines.

Thread Thread
 
hamidmolareza profile image
Hamid Molareza

Done.
Please be kind. :)

Some comments have been hidden by the post's author - find out more