Differences between the Sealed class and Static class in C#

In this article, we will discuss the difference between the Sealed and Static class in C#. But before discussing the differences, we must know about the Sealed and Static class in C# with their syntax and examples.

What is the Sealed class in C#?

The sealed class in C# is a class, which avoids inheritance. If there are two classes that are parent class and child class and the parent class is marked as the Sealed, the child class can not inherit the properties and methods of the parent in the child class. It means a sealed class cannot be used as the parent or base class.

Syntax of the Sealed Class:

It has the following syntax:

Whenever a class is sealed, it cannot be extended because it does not allow inheritance. It means that this class cannot be changed in the future. This sealed class is used when the programmer wants to finalize the class. By seeing the sealed class, the compiler will avoid the overhead of runtime checks for calling other methods, etc. It makes the class execute faster.

Example:

Let us take a program to illustrate the Sealed Class in C#.

Output:

Differences between the Sealed class and Static class in C#

Explanation:

The above program has two classes, the Vehicle and the Program class. Here, the Vehicle is sealed, meaning the properties of this class cannot be inherited by other classes. In the Vehicle class, there is a method named Drive, which will print some statements. There is a commented code where another class, Car, is trying to inherit the class Vehicle. It throws the compilation error, saying the sealed classes cannot be inherited after commenting on that part of the code. We can access the properties in the Program class by creating the object for the Vehicle class.

What is the Static class in C#?

The static class in C# is a class that cannot be instantiated. In other words, a programmer cannot create an object or an instance of the static class. It is the same as normal classes, but one main thing is that we cannot create objects of this static class.

This static class contains the static members and member functions, which means the variables or fields, events, nested types, and methods are all static. These classes are mainly used to define utility functions that do not need any instance to access those static methods. They cannot be inherited. Static classes can give better runtime optimization and reduce memory overhead because new objects are not created.

The syntax of the static class:

It has the following syntax:

Example:

Let us take a program to illustrate the static class in C#.

Output:

Differences between the Sealed class and Static class in C#

Explanation:

The above program has two classes: the MathHelper class, which is static, and the Main class. The MathHelper class is static, meaning we cannot create objects for that class. This class contains only static methods. In MathHelper, there are two classes: Add and Subtract methods. In the main class, the Add method is accessed by using the static class names and stored in the variable. The Subtract method is also accessed by using its class name. After that, both the results are printed.

Differences between the Sealed class and Static class in C#

Differences between the Sealed class and Static class in C#

There are several difference between the Sealed and Static class in C#. Some main differences between the Sealed and Static class in C# are as follows:

There are several difference between the Sealed and Static class in C#. Some main differences between the Sealed and Static class in C# are as follows:

FeatureSealed classStatic class
InheritanceIt cannot be inherited.Static classes are also cannot be inherited.
InstantiationWe can able to create the objects for the sealed class.Object creation is not possible for the static class.
Members in classThe member of the sealed class can be both static and non-static.The members of the static class, methods and properties should be static only.
UsageIt is used to finalize the class.These are mainly used for utility methods which does not need objects to access.
ExtensionThese classes cannot be further changed or extended.These cannot be inherited and its members are shared.
PerformanceThis class will give better performance in terms of runtime.This class will give better performance in terms in runtime and memory.
ConstructorConstructor can be used to initialize the variables.There is no constructor used here due to the elimination of the object creation.
Access ModifierIt can have different access modifiers for members of the class.Here, all the members should be static, and the access modifier should be the same.
Memory allocationMemory is allocated for objects of the sealed class.No memory is allocated.
PolymorphismSupports polymorphism.Do not support the polymorphism.





Latest Courses