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.
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.
See more on this question at Stackoverflow