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 Class | Static Class | |
---|---|---|
Inheritance | Nope | Nope |
Instantiation | You can create objects of a Sealed class | You cannot create objects of a static class |
Members | It can contain regular members like fields, methods and constructors. It can also contain static members like any other concrete class | A static class cannot contain any non-static members or methods. It can only contain static members, methods and constants. |
Usage | You 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 objects | You 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 Allocation | Similar to concrete classes, these are stored in Heaps and are Garbage Collected | Static members are stored when the application starts and are placed in a separate memory called Data Segments. |