In C#, what do abstract and sealed enforce on a class and on an override, and when can base.Method() be called?
answer
- must specialise vs may not
- no body, implicitly virtual
- sealed only together with override
- abstract base member has nothing to call
basics
~20 sAn abstract class cannot be instantiated and its abstract members must be overridden by concrete subclasses. A sealed class cannot be derived from, and sealed override stops further overriding. base.Method() calls the base implementation, unless that member is abstract.
solid answer
~40 sAn `abstract` class cannot be created with `new` (CS0144) but can hold state, constructors and implemented members. An `abstract` member has no body, is implicitly virtual, and every non-abstract subclass must override it or get CS0534. A `sealed` class cannot be a base class (CS0509), and `sealed override` keeps an override in place while forbidding further overrides (CS0239); `sealed` on a member that is not an override is CS0238. `base.Format(body)` calls the base class's implementation without virtual dispatch, which is how an override extends behaviour. It is rejected with CS0205 when the base member is abstract, since there is no body to run.
code
csharp · 21 linesNotification n = new HtmlEmailNotification();
Console.WriteLine(n.Format("Invoice ready"));
n.Send();
abstract class Notification
{
public abstract void Send();
public virtual string Format(string body) => body.Trim();
}
class EmailNotification : Notification
{
public override void Send() => Console.WriteLine("SMTP send");
public sealed override string Format(string body) => "Subject: " + base.Format(body);
}
class HtmlEmailNotification : EmailNotification
{
// public override string Format(string body) => body; // CS0239: Format is sealed in EmailNotification
public override void Send() { Console.Write("HTML + "); base.Send(); }
}go deeper
Recall the basic rules: abstract classes cannot be instantiated, abstract members have no body and must be overridden, sealed classes cannot be inherited.
Explain sealed override versus a sealed class, why sealed needs override on a member, and that base calls bypass virtual dispatch. Know CS0534, CS0509 and CS0239 by meaning.
Use sealed override deliberately to lock an invariant such as a shared subject line while leaving the class open, and spot a base call to an abstract member before the compiler does.
Weigh where a hierarchy's extension points sit: which members stay virtual, which overrides get sealed, and what each open member commits the team to supporting.
## Two modifiers, opposite intents `abstract` and `sealed` are the two ends of the same question: **must** a class or member be specialised, or **may it not** be? Both are checked entirely by the compiler, and each has its own diagnostics. The notification system makes a good test bed: `Notification` cannot send anything by itself, `EmailNotification` is a finished sender, and the subject-line formatting it adds must not be changed by its subclasses. ## abstract on a class and on a member An **abstract class** is a base class that cannot be instantiated on its own. - `new Notification()` fails with **CS0144**, "Cannot create an instance of the abstract type or interface". - It can still hold fields, implemented methods, properties and constructors. Those constructors run when a derived object is built; they are usually `protected`. - It does not need to declare any abstract member; `abstract` on the class alone just forbids direct instantiation. An **abstract member** is a contract with no body. 1. It has no body: `public abstract void Send();`. Writing a body is **CS0500**. 2. It may appear only in an abstract class; inside a non-abstract class it is **CS0513**. 3. It is implicitly virtual, so derived classes implement it with `override`. 4. It cannot be `private` (**CS0621**) and, in a class, cannot be `static` (**CS0112**). Static abstract members exist only on interfaces, a separate topic. 5. Every non-abstract derived class must override every inherited abstract member, or the compiler reports **CS0534**, "does not implement inherited abstract member". An abstract class may also **re-abstract** an inherited virtual member with `abstract override`, forcing its own subclasses to supply a fresh implementation. ## sealed on a class and on an override - A **sealed class** cannot be a base class: `class SilentPush : PushNotification` fails with **CS0509**, "cannot derive from sealed type". Structs are implicitly sealed. - `abstract sealed class` is contradictory and is **CS0418**, "an abstract type cannot be sealed or static". - A sealed class may still override inherited virtual members, but declaring a **new** virtual member in it is **CS0549**, and a new `protected` member draws warning **CS0628**, because nobody can derive to use either. - On a member, `sealed` is legal only together with `override`. `sealed override` keeps the override in force while stopping classes further down from overriding it again; an attempt is **CS0239**, "cannot override inherited member ... because it is sealed". `sealed` on a method that is not an override is **CS0238**. | Declaration | What it means | Diagnostic if broken | |---|---|---| | `abstract class Notification` | Cannot be instantiated | CS0144 on `new` | | `public abstract void Send();` | Must be overridden by concrete subclasses | CS0534 | | `sealed class PushNotification` | Cannot be derived from | CS0509 | | `public sealed override string Format(...)` | Cannot be overridden further | CS0239 | | `public sealed void Retry()` | Not allowed: nothing to seal | CS0238 | ## base calls `base.Member(...)` calls the **base class's implementation** of a member from inside a derived class, without virtual dispatch. That is what lets an override extend rather than replace behaviour: - `EmailNotification.Format` returns `"Subject: " + base.Format(body)`, reusing the trimming that `Notification` does. - `HtmlEmailNotification.Send` writes its own prefix and then calls `base.Send()`, which runs `EmailNotification.Send`, the nearest implementation up the chain. - Calling `base.Send()` when the base member is **abstract** is **CS0205**, "Cannot call an abstract base member", because there is no body to run. - `base` reaches only one level of implementation; there is no syntax for skipping to a grandparent's version when the parent overrides it. ## Reading the example With `Notification n = new HtmlEmailNotification();`, `n.Format("Invoice ready")` dispatches to the sealed override in `EmailNotification`, which calls the base trimming and prints `Subject: Invoice ready`. `n.Send()` dispatches to `HtmlEmailNotification.Send`, which prints `HTML + ` and then runs the email sender through `base.Send()`. The commented-out override of `Format` would fail with CS0239, which is exactly the guarantee `EmailNotification` asked for: every email, HTML or not, gets the same subject line.
- Can an abstract class turn an inherited virtual method back into an abstract one?Yes. `public abstract override string Format(string body);` in an abstract intermediate class re-abstracts the member: the inherited body is no longer the default, and every concrete class below must supply its own override or get CS0534. It is useful when a mid-level base knows the base default is wrong for its whole family.
- Why does the compiler reject a new virtual or protected member in a sealed class?Both only make sense for derived classes, and a sealed class has none. A new `virtual` member is error CS0549; a new `protected` member is warning CS0628, since it is effectively private. A sealed class can still override inherited virtual members such as `ToString`, which is ordinary and allowed.
- Says an abstract class cannot have constructors or fields.
- Thinks a class must declare an abstract member to be marked abstract.
- Believes sealed can be put on any method to stop subclasses overriding it.
- Claims a sealed class cannot override inherited virtual members such as ToString.
- Expects base.Send() to run the most derived override through virtual dispatch.