Reputation: 2969
Is there any class that be implemented in static class? means:
static class ABC : Anyclass
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?
Upvotes: 165
Views: 108722
Reputation: 11
public class BaseClassDemo
{
}
//Note
//A static class can be used as a convenient container for sets of
//methods that just operate on input parameters and do not have to
//get or set any internal instance fields.
//The advantage of using a static class is that the compiler can
//check to make sure that no instance members are accidentally
//added. The compiler will guarantee that instances of this class
//cannot be created.
//Static class 'static type' cannot derive from type 'type'. Static
//classes must derive from object.
public static class StaticClassDemo : BaseClassDemo //Error
public static class StaticClassDemo
{
//Static class must have static members
public static void Ram()
{
throw new NotImplementedException();
}
}
//Sealed class can inherit from the base class.
public sealed class SealedClassDemo : BaseClassDemo
{
}
//We can't derive from sealed class.
public class derivedClass:SealedClassDemo //Error
public partial class RandomClass2
{
//we can't create instance of static class.
StaticClassDemo demo = new StaticClassDemo() //Error
}
Upvotes: 1
Reputation: 736
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();
}
}
Upvotes: 4
Reputation: 292
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
Upvotes: 6
Reputation: 32511
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 |
+--------------+---+-------------------------+------------------+---------------------+
Upvotes: 699
Reputation: 147
1 - No, you can't implement a static class.
2 - No, you can't inherited from a static or sealed class
Upvotes: 0
Reputation: 26446
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.
Upvotes: 20
Reputation: 20585
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.
Upvotes: 40