Ahmad
Ahmad

Reputation: 13426

What is the difference between a non-virtual method and a sealed method?

I have a confusion I'd like to resolve .. In C#, only base class methods with the virtual tag can be overridden in derived classes. Base class methods without the virtual tag cannot be overridden. At best, derived classes can hide base class methods using new. Then we have sealed methods, which also cannot be overridden in base classes.

So then, what is the difference between a sealed method, and a standard non-virtual method ?

Another related question. Can base class sealed methods be replaced in derived classes using new ?

Upvotes: 10

Views: 2772

Answers (4)

Steven Hoffman
Steven Hoffman

Reputation: 251

The original poster asked two different questions:

  1. What is the difference between a sealed method and a standard non-virtual method?
  2. Can base class sealed methods be replaced in derived classes using new?

Question 1 - Difference between sealed and non-virtual methods

If you don't want a child class to be able to access a new method that you are adding, creating non-virtual methods is the ideal solution. As an example, you may add a non-virtual method to a class that wasn't in its parent class.

However, sometimes your class's parent class has defined a method as virtual but you don't want any of your class's children to be able to override your specific override of this method. This is when you use sealed. It tells the compiler that the once virtual method can no longer be overriden. See the docs on sealed here.

Question 2 - Can base class sealed methods be replaced using new

Yes, they can, though this may not do what you think. Replacing a sealed method does mean that calling that method will now call your implementation. However, if your class is ever cast into a reference to the parent class, the parent class's implementation of the method will be used, not yours. The Microsoft docs explain this here.

The following example is slightly modified from Microsoft's docs:

public class A
{
    public virtual void DoWork() { Console.WriteLine("A"); }
}

public class B : A
{
    public override void DoWork() { Console.WriteLine("B"); }
}

public class C : B
{
    public sealed override void DoWork() { Console.WriteLine("C"); }
}

public class D : C
{
    // Uncommenting the following line would cause a compilation error, since DoWork is sealed
    // public override void DoWork() { Console.WriteLine("D"); }
    
    public new void DoWork() {  Console.WriteLine("D"); }
}

...

// Later on in a class
D d = new D();
d.DoWork(); // Prints "D"

C c = d;
c.DoWork(); // Prints "C"

Upvotes: 3

Decoy
Decoy

Reputation: 1597

Sealed methods can only be methods that override a method from the base class so no further overriding is possible.

From the docs :

When an instance method declaration includes a sealed modifier, that method is said to be a sealed method.
If an instance method declaration includes the sealed modifier, it must also include the override modifier.

This is not required for virtual methods.

Upvotes: 2

John Koerner
John Koerner

Reputation: 38077

sealed prevents any further overriding of the virtual methods up the chain. You can only define sealed on methods that are overidden. Take a look at the docs for sealed: http://msdn.microsoft.com/en-us/library/aa645769(v=vs.71).aspx

They give a great example of sealed usage:

using System;
class A
{
   public virtual void F() {
      Console.WriteLine("A.F");
   }
   public virtual void G() {
      Console.WriteLine("A.G");
   }
}
class B: A
{
   sealed override public void F() {
      Console.WriteLine("B.F");
   } 
   override public void G() {
      Console.WriteLine("B.G");
   } 
}
class C: B
{
   override public void G() {
      Console.WriteLine("C.G");
   } 
}

In this case anyone who derives off of B can override G, but not F.

Upvotes: 12

LB2
LB2

Reputation: 4860

If I read this correctly, sealed allows to stop virtual from being virtual. Essentially undoes virtual.

Upvotes: 3

Related Questions