Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

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 ?

like image 738
Ahmad Avatar asked Feb 26 '14 15:02

Ahmad


People also ask

What is non-virtual method?

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.

What is meant by sealed How is it different from virtual?

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.

What are virtual and non-virtual methods?

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.

What is the difference between virtual method and abstract method?

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.


1 Answers

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"
like image 90
Steven Hoffman Avatar answered Nov 16 '22 02:11

Steven Hoffman