Static and Sealed class differences

C#.NetOopStaticSealed

C# Problem Overview


  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?

C# Solutions


Solution 1 - C#

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 - C#

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 - C#

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 - C#

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

Solution 5 - C#

Simple answer is a sealed class cannot be used as a base class.

I'm trying to show you sealed class is a derived class in the below code

 public sealed class SealedClass : ClassBase
{
    public override void Print()
    {
        base.Print();
    }
}

and another sealed feature is only accessible with instance from it.(you can not inherit from it)

 class Program
{
    static void Main(string[] args)
    {
        SealedClass objSeald = new SealedClass();
        objSeald.Name = "Blah blah balh";
        objSeald.Print();

    }
}

Solution 6 - C#

1 - No, you can't implement a static class.

2 - No, you can't inherited from a static or sealed class

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSaurabh MahajanView Question on Stackoverflow
Solution 1 - C#Hossein Narimani RadView Answer on Stackoverflow
Solution 2 - C#Parimal RajView Answer on Stackoverflow
Solution 3 - C#C.EvenhuisView Answer on Stackoverflow
Solution 4 - C#AbdulView Answer on Stackoverflow
Solution 5 - C#Amin SaadatiView Answer on Stackoverflow
Solution 6 - C#SmaiilView Answer on Stackoverflow