Static vs Sealed Class – Comparison in an easy way

Let us know about the features of sealed class and static class in C# and summarize the important differences - static vs sealed class

Sealed Classes and Static Classes serve special purposes in C#, and are used in different scenarios. Let us know in detail about these Object Oriented Programming structures and understand the key differences between them.

What is a Sealed Class?

The primary feature of a Sealed class is its behavior with inheritance.

A sealed class cannot be inherited, used to prevent other class from extending its behavior. But you can create an instance of a sealed class like any other concrete class.

A Sealed class can have members like fields, properties and methods. However. these members can only be accessed via objects. It can also contain static members like any other class.

Sealed classes are similar to concrete classes and are memory allocated in heap, and are Garbage Collected for removal from memory.

Sealed Classes are used when you want to create a class that is not intended to be a base class for extension. Since there is no inheritance here, you can ensure the behavior of the class is not modified by another derived type via Inheritance.

While creating a Singleton class, you mark the class as Sealed so that no other class can extend the singleton class.

What is a Static Class?

A static class is meant for encapsulating utility or helper behavior, which don’t maintain any state.

These hold such behavior which are common across application, and which doesn’t need an object to access. Their behavior doesn’t change between instances.

Examples such as Math, Date Time utilities and so on.

A static class can contain only static methods, variables and constants. It is implicitly sealed, you cannot inherit a static class. you cannot also instantiate a static class with the new keyword.

These are loaded into the memory when the application starts. static members are maintained in a special memory area called Static Data Area or Data Segment.

using System;

public static class MathUtility
{
    public static int Add(int a, int b)
    {
        return a + b;
    }

    public static int Subtract(int a, int b)
    {
        return a - b;
    }

    public const double Pi = 3.14159;
}

public sealed class Circle
{
    private double radius;

    public Circle(double radius)
    {
        this.radius = radius;
    }

    public double CalculateArea()
    {
        return MathUtility.Pi * radius * radius;
    }
}

// Attempt to create a derived class (this will result in a compilation error)
// public class DerivedCircle : Circle { /* Compilation error: Cannot derive from sealed type 'Circle' */ }

Summary – difference between static class and sealed class

Sealed ClassStatic Class
InheritanceNopeNope
InstantiationYou can create objects of a Sealed classYou cannot create objects of a static class
MembersIt can contain regular members like fields, methods and constructors. It can also contain static members like any other concrete classA static class cannot contain any non-static members or methods. It can only contain static members, methods and constants.
UsageYou use a Sealed class to encapsulate behavior that should not be modified by inheritance. In other words, you should not allow any other class to access behavior except via objectsYou use a static class to encapsulate behavior that is common across application, doesn’t change with objects and doesn’t involve saving any state. Best suited for organizing utilities and helper methods that perform some business logic.
Memory AllocationSimilar to concrete classes, these are stored in Heaps and are Garbage CollectedStatic members are stored when the application starts and are placed in a separate memory called Data Segments.

Buy Me A Coffee

Found this article helpful? Please consider supporting!

Ram
Ram

I'm a full-stack developer and a software enthusiast who likes to play around with cloud and tech stack out of curiosity. You can connect with me on Medium, Twitter or LinkedIn.

Leave a Reply

Your email address will not be published. Required fields are marked *