Why Abstract class can not be sealed or static ? and I am also confuse about this question Why declare static classes as sealed and abstract in C#?
static
class cannot be marked sealed
because it is made sealed
by compiler by default.
Static classes are sealed and therefore cannot be inherited.
static
class cannot be marked as abstract
, because it would be pointless. abstract
class makes sense when you want all derived classes to implement same part of the logic. But because static
class cannot be derived there is no way other class will implement these gaps.
They cannot inherit from any class except
Object
.
Both quotes from Static Classes and Static Class Members (C# Programming Guide) .
C# specification is a little more detailed about that:
10.1.1.3 Static classes
A
static
class may not include asealed
orabstract
modifier. Note, however, that since astatic
class cannot be instantiated or derived from, it behaves as if it was bothsealed
andabstract
.
You can read what does it mean for class to be sealed
or abstract
:
An abstract class cannot be instantiated directly, and it is a compile-time error to use the new operator on an abstract class
The sealed modifier is used to prevent derivation from a class. A compile-time error occurs if a sealed class is specified as the base class of another class.
Update
And a word about an article linked in the query you mentioned ( Static Class Design ). It's already stated in accepted answer from that question. If you read carefully you can see that:
√ DO declare
static
classes assealed
,abstract
, and add aprivate
instance constructor if your programming language does not have built-in support for static classes .
.NET (so C# as well) do have built-in support for static classes, so you don't have (and even can't) to make your classes pseudo-static by marking it both sealed
and abstract
.
First of all, let's start with a definition; sealed is a modifier which if applied to a class make it non-inheritable and if applied to virtual methods or properties makes them non-ovveridable.
public sealed class A { ... }
public class B
{
...
public sealed string Property { get; set; }
public sealed void Method() { ... }
}
An example of its usage is to define a specialized class/method or property in which potential alterations can make them stop working as expected (for example, the Pens class of the System.Drawing namespace).
...
namespace System.Drawing
{
//
// Summary:
// Pens for all the standard colors. This class cannot be inherited.
public sealed class Pens
{
public static Pen Transparent { get; }
public static Pen Orchid { get; }
public static Pen OrangeRed { get; }
...
}
}
Because a sealed class cannot be inherited, it cannot be used as base class and by consequence, an abstract class cannot use the sealed modifier.
It's also important to mention that structs are implicitly sealed.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.