Static and Sealed class differences

  1. Is there any class that be implemented in static class? means:

    static class ABC : Anyclass
    
  2. Is there any class which can be inherited in both sealed class and static class?
    means:

    static class ABC : AClass {}
    

    And

    sealed class ABC : AClass {}
    

May I be wrong in some extent?


Solution 1:

This may help you:

+--------------+---+-------------------------+------------------+---------------------+
|  Class Type  |   | Can inherit from others | Can be inherited | Can be instantiated | 
|--------------|---|-------------------------+------------------+---------------------+
| normal       | : |          YES            |        YES       |         YES         |
| abstract     | : |          YES            |        YES       |         NO          |
| sealed       | : |          YES            |        NO        |         YES         |
| static       | : |          NO             |        NO        |         NO          |
+--------------+---+-------------------------+------------------+---------------------+

Solution 2:

In simple words

Static Class

A class can be declared static, indicating that it contains only static members. It is not possible to create instances of a static class using the new keyword. Static classes are loaded automatically by the .NET Framework common language runtime (CLR) when the program or namespace containing the class is loaded.

Sealed Class

A sealed class cannot be used as a base class. Sealed classes are primarily used to prevent derivation. Because they can never be used as a base class, some run-time optimizations can make calling sealed class members slightly faster.

Solution 3:

You can let a sealed class inherit from another class, but you cannot inherit from a sealed class:

sealed class MySealedClass : BaseClass // is ok
class MyOtherClass : MySealedClass     // won't compile

A static class cannot inherit from other classes.

Solution 4:

You can simply differentiate both of them as:

       Sealed Class       |        Static Class
--------------------------|-------------------------
it can inherit From other | it cannot inherit From other
classes but cannot be     | classes as well as cannot be
inherited                 | inherited