Reputation: 13426
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
Reputation: 251
The original poster asked two different questions:
sealed
method and a standard non-virtual method?sealed
methods be replaced in derived classes using new
?sealed
and non-virtual methodsIf 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.
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
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
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