difference between static and sealed class

Asked 8 years ago Updated 12 days ago 2234 views

2 Answers


0

Both static and sealed classes restrict inheritance in C#, but they serve very different purposes.

Static Class

A static class is a class that:

  • Cannot be instantiated
  • Cannot contain instance members
  • Can only contain static members
  • Is automatically sealed by the compiler

Example

// Static class declaration
public static class MathHelper
{
    // Static method
    public static int Add(int a, int b)
    {
        return a + b;
    }
}

Usage

// Calling static method directly using class name
int result = MathHelper.Add(5, 3);

Key Characteristics of Static Classes

Feature Static Class
Can create object? No
Can inherit? No
Can be inherited? No
Contains instance members? No
Contains static members? Yes
Constructor allowed? Only static constructor

When to Use Static Classes

Use static classes when you need:

  • Utility/helper methods
  • Global constants
  • Shared logic
  • Extension methods

Examples:

  • Math
  • Console
  • File

Sealed Class

A sealed class is a normal class that:

  • Can be instantiated
  • Can contain instance and static members
  • Cannot be inherited by another class

Example

// Sealed class declaration
public sealed class Employee
{
    // Instance property
    public string Name { get; set; }

    // Instance method
    public void Display()
    {
        Console.WriteLine(Name);
    }
}

Usage

// Creating object of sealed class
Employee emp = new Employee();

// Setting property value
emp.Name = "John";

// Calling method
emp.Display();

Attempting Inheritance

// ❌ Error: Cannot derive from sealed type
public class Manager : Employee
{
}

Key Characteristics of Sealed Classes

Feature Sealed Class
Can create object? Yes
Can inherit from other class? Yes
Can be inherited? No
Contains instance members? Yes
Contains static members? Yes
Supports constructors? Yes

Main Difference

Static Class Sealed Class
Cannot be instantiated Can be instantiated
Only static members allowed Both instance & static members allowed
Cannot inherit or be inherited Can inherit, but cannot be inherited
Used for utility/helper functionality Used to stop further inheritance
Automatically sealed Explicitly marked sealed

Real-World Analogy

Static Class

Think of a toolbox:

  • You don't create an object of the toolbox
  • You directly use tools from it

Example:

Math.Sqrt(25);

Sealed Class

Think of a final product:

  • You can use it
  • But nobody can modify or extend it further

Important Note

A static class is internally treated as:

  • abstract
  • sealed

This combination means:

  • Cannot instantiate (abstract)
  • Cannot inherit (sealed)

But C# provides the static keyword for cleaner syntax and intent.

Quick Summary

Use static when:

  • You only need shared methods/data
  • No object creation required

Use sealed when:

  • You want normal objects
  • But want to prevent inheritance
0
Difference between Static class and sealed class:
Static class:
1) static class is those class that can’t be instantiated. Means you can't create the instance of any static class.
2) static class can only contain static members or function.
3) Static classes can only have a static constructor to initialize static members function.
4) Static classes are sealed so they can’t be inherited.
6) static class is defined using “static’ keyword.
Example -        static class class_name

Sealed Class:
1) Sealed Class can’t be inherited means When you define any class as sealed class it’s not possible to inherit that class.
2) In Inheritance feature sealed class is the last class.
3) You can’t define an abstract method or abstract class in sealed classes.
4) The sealed class is defined using “sealed’ keyword.
Example-    sealed class class_name

Write Your Answer