5

From msdn sealed (C# Reference)

"When applied to a method or property, the sealed modifier must always be used with override."

Why must it always be used with override?

LarsTech
  • 80,625
  • 14
  • 153
  • 225
José D.
  • 4,175
  • 7
  • 28
  • 47
  • 7
    Without being marked as `virtual` in a base class to allow for `override`, the method is already effectively sealed. Explicitly sealing a method only matters when overriding and not wanting further overrides. – Jonathan Lonowski Jan 23 '14 at 18:17

4 Answers4

10

sealed prevents a method from being overriden by subclasses. If the method marked as sealed wasn't overridable in the first place, why would you mark it as sealed?

dcastro
  • 66,540
  • 21
  • 145
  • 155
3

Because there is not reason to add it to a property that does not override a property from another class. It you put the sealed modifier on a derived class's property, it is saying the anyone that derives from you cannot further override that property. If the property was never overridable to start with, there is no point in using sealed.

Basically, it is saying that subclasses must use the property the way you intended.

clhereistian
  • 1,261
  • 1
  • 11
  • 19
3

Because structs are implicitly sealed, they cannot be inherited, "sealed" prevents a method from being overriden by subclasses.

See the example: In the following example, Z inherits from Y but Z cannot override the virtual function F that is declared in X and sealed in Y.

class X
{
    protected virtual void F() { Console.WriteLine("X.F"); }
    protected virtual void F2() { Console.WriteLine("X.F2"); }
}

Class Y inherits from class X, and define function F() as: sealed protected override void F().

class Y : X
{
    sealed protected override void F() { Console.WriteLine("Y.F"); }
    protected override void F2() { Console.WriteLine("Y.F2"); }
}

Class Z that inherits from Y where function F() was defined as sealed, you can´t override the function because its defined as "sealed"

class Z : Y
{
    // Attempting to override F causes compiler error CS0239. 
    // protected override void F() { Console.WriteLine("C.F"); }

    // Overriding F2 is allowed. 
    protected override void F2() { Console.WriteLine("Z.F2"); }
}

more info: sealed (C# Reference)

Jorgesys
  • 124,308
  • 23
  • 334
  • 268
0

Say you have:

public BaseClass
{
    public virtual void SomeMethod() 
    {
    }
}

And:

public MyDerivedClass : BaseClass
{
     public void AnotherMethod()
     {
         // there's no point sealing this guy - it's not virtual
     }

     public override sealed void SomeMethod()
     {
         // If I don't seal this guy, then another class derived from me can override again
     }
}

And then:

public class GrandChildClass : MyDerivedClass
{
    public override void AnotherMethod()
    {
        // ERROR - AnotherMethod isn't virtual
    }

    public override void SomeMethod()
    {
        // ERROR - we sealed SomeMethod in MyDerivedClass
        // If we hadn't - this would be perfectly fine
    }
}
Matt Burland
  • 44,552
  • 18
  • 99
  • 171