priyanka.sarkar
priyanka.sarkar

Reputation: 26518

Override Sealed is valid but why not Virtual Sealed in C#?

The below code snippet is valid

public class BaseClass
{

   public virtual void Display()
   {
      Console.WriteLine("Virtual method");
   }
 }

public class DerivedClass : BaseClass
{

  public override sealed void Display()
  {
       Console.WriteLine("Sealed method");
  }

}

But why not

public class BaseClass
{

 public virtual sealed void Display()
 {
          Console.WriteLine("Virtual method");
 }
}

Edited

Actually I was reading What is sealed class and sealed method? this article. So I was following the author's instruction. Suddenly I tried to play the concept of Sealed with the base class. That's why I came up with this question.

Upvotes: 2

Views: 171

Answers (3)

Matthew Haugen
Matthew Haugen

Reputation: 13286

I'm not really sure what you'd expect that to even do.

The sealed keyword means you can't inherit it.

You can also use the sealed modifier on a method or property that overrides a virtual method or property in a base class. This enables you to allow classes to derive from your class and prevent them from overriding specific virtual methods or properties.

The virtual keyword means you can.

The virtual keyword is used to modify a method, property, indexer, or event declaration and allow for it to be overridden in a derived class. For example, this method can be overridden by any class that inherits it:

I'm not sure why you'd need or want both.

When applying it to override, that makes sense. That means "you can't override this behavior," and effectively overrules the previous (relative to the hierarchy) virtual keyword on the method or property.

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1502616

override sealed is valid because it says "I'm overriding a base class method, but derived classes can't override me." That makes sense. One part of it is talking about the relationship to its base class; the other is talking about the relationship to derived classes.

virtual sealed would be saying "You can override me (virtual) but you can't override me (sealed)." That makes no sense. The two modifiers are contradictory and apply to the same relationship.

Upvotes: 16

Yuval Itzchakov
Yuval Itzchakov

Reputation: 149598

Sealed:

You can also use the sealed modifier on a method or property that overrides a virtual method or property in a base class. This enables you to allow classes to derive from your class and prevent them from overriding specific virtual methods or properties.

Override:

The override modifier is required to extend or modify the abstract or virtual implementation of an inherited method, property, indexer, or event.

Sealed means it is the "end-of-the-hierarchy" for the given method/class. Making a method virtual is merely saying "this is the default behavior, override me if you want different behavior" and sealed which means "this cannot be overridden ever" is a paradox.

Upvotes: 4

Related Questions