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
?
Methods that don't have either the virtual or override keywords, or that have the new keyword, are said to be non-virtual. When a virtual method is invoked on an object, the run-time type of the object is used to determine which implementation of the method to use.
The sealed keyword can be used before the access modifier, before or after the override keyword. If you want to declare a method as sealed, then it has to be declared as virtual in its base class because a non-virtual method cannot be overridden.
Non-virtual functions are resolved statically at Compile-time , While Virtual functions are resolved dynamically at Run-time . In order to achieve this flexibility of being able to decide which function to call at run-time, there is an little overhead in case of virtual functions.
Virtual methods have an implementation and provide the derived classes with the option of overriding it. Abstract methods do not provide an implementation and force the derived classes to override the method. So, abstract methods have no actual code in them, and (non-abstract) subclasses HAVE TO override the method.
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With