atoipowered
atoipowered

Reputation: 506

Why use sealed on a method or property whenever the current class IS a subclass

Why shall we use sealed keyword on a method or property whenever the current class once inherited from a super class? Suppose we made a class and tend to expose one or more of its methods to the object user but not letting it being inherited at all and use sealed to solve the issue. Then, why not? And what's the reason behind sealing just a current-inherited class's methods or properties?

Upvotes: 3

Views: 3540

Answers (4)

Derek W
Derek W

Reputation: 10026

As stated in the MSDN Docments on 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.

In other words, you can stop the overriding from happening further down the class inheritance hierarchy. As a programmer, you are basically saying that this particular method should have common functionality with all subclasses.

Here's a great code example from the same article:

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

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

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"); }
}

UPDATE Per Request For Additional Clarification

Here's a less abstract example of a possible application for a sealed method.

abstract class Car
{
    public abstract void Make();
}

class Ford : Car
{
    // We don't want someone inheriting from this class to change the
    // 'Make' functionality - so we seal the method from being further
    // overridden down the inheritance hierarchy 
    sealed public override void Make() { Console.WriteLine("Ford"); }
}

// This way there is no way (besides shadowing) someone inheriting from Ford
// can change the behavior of Make() - so these two types will contain the 
// same behavior. Pretty nice, eh!? 
class Focus : Ford
{ 
}

class Escape : Ford
{
}

Upvotes: 6

Lasse V. Karlsen
Lasse V. Karlsen

Reputation: 391566

There are 2 primary reasons for sealing a class or a method.

The first, and the most important one (probably) is that you disallow inheritance on the type or method in question. This can be important in some cases.

The second reason is that the compiler can apply optimizations it otherwise could not have done.

Doing a virtual method lookup is not a very costly affair, but it does incur some overhead. If the compiler knows that:

  1. You're using the most specific class you can in the given situation
  2. The given type or method is sealed

then in some cases it can compile a call to a method into a direct call to the correct method, and not a lookup through the virtual method table of the type. This will remove the overhead of the virtual method lookup in this situation.

Upvotes: 4

Jeppe Stig Nielsen
Jeppe Stig Nielsen

Reputation: 62002

You apply sealed to an overridden method (or property etc.) to disallow classes that derive from your class, overriding that method further.

Upvotes: 0

Stilgar
Stilgar

Reputation: 23571

As stated in the docs ( http://msdn.microsoft.com/en-us/library/88c54tsw.aspx ) this prevents a method from being virtual. It is useful because override keyword implies virtual. If you want to override a method but stop further overriding you can add sealed to the method definition.

If the class itself is sealed there is no reason to seal a specific method since the class cannot be inherited anyway.

Upvotes: 1

Related Questions