The Difference Between Virtual, Override, New and Sealed Override

The difference between virtual, override, new and sealed override

The virtual keyword is used to modify a method, property, indexer or event declaration, and allow it to be overridden in a derived class. For example, this method can be overridden by any class that inherits it:
Use the new modifier to explicitly hide a member inherited from a base class. To hide an inherited member, declare it in the derived class using the same name, and modify it with the new modifier.

This is all to do with polymorphism. When a virtual method is called on a reference, the actual type of the object that the reference refers to is used to decide which method implementation to use. When a method of a base class is overridden in a derived class, the version in the derived class is used, even if the calling code didn't "know" that the object was an instance of the derived class. For instance:

public class Base
{
public virtual void SomeMethod()
{
}
}

public class Derived : Base
{
public override void SomeMethod()
{
}
}

...

Base d = new Derived();
d.SomeMethod();

will end up calling Derived.SomeMethod if that overrides Base.SomeMethod.

Now, if you use the new keyword instead of override, the method in the derived class doesn't override the method in the base class, it merely hides it. In that case, code like this:

public class Base
{
public virtual void SomeOtherMethod()
{
}
}

public class Derived : Base
{
public new void SomeOtherMethod()
{
}
}

...

Base b = new Derived();
Derived d = new Derived();
b.SomeOtherMethod();
d.SomeOtherMethod();

Will first call Base.SomeOtherMethod , then Derived.SomeOtherMethod . They're effectively two entirely separate methods which happen to have the same name, rather than the derived method overriding the base method.

If you don't specify either new or overrides, the resulting output is the same as if you specified new, but you'll also get a compiler warning (as you may not be aware that you're hiding a method in the base class method, or indeed you may have wanted to override it, and merely forgot to include the keyword).

An overriding property declaration may include the sealed modifier. Use of this modifier prevents a derived class from further overriding the property. The accessors of a sealed property are also sealed.

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

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.

What is real time use virtual and new,override methods in c#..?

You have a method with this signature:

void Print(ICollection<A> objects)
{
foreach(var obj in objects)
{
obj.print();
}
}

the print will use either B implementation or A implementation (for A it could be another child C that does not new the print method)
reading: https://stackoverflow.com/a/6162547/6460438

Can't use virtual and override on the same method in C#

You can declare a certain method as virtual only once, but you can override it as many times as you want - an override is not final, and it does not limit classes that inherit from the first overriding class. The method that will eventually execute is the last one the overrides the virtual method. So your code does behave as expected.

C# is very verbose with regard to overriding - you have more specifiers than C++ or Java. It is so to let the programmer specify the exact intent:

  • You use virtual to specify a method can be overridden by subclasses.
  • You use override to specify you are overriding a method that you know is virtual (if it's not, the compiler will report an error).
  • You use sealed to prevent further overriding.
  • And you use new to hide instead of override.

This can be confusing and sometimes annoying, but it ensures you actually know what you're doing, and it makes your intention self-documented.

What is the point of Virtual and Override? Doesn't C# Do the same thing without them?

My confusion is that I just tested not using the virtual keyword in
the base class' method definition and not including that method in the
derived class, and I was still able to call the base class' method (it
showed up in intellisense and ran).

This is expected behaviour.

Virtual allows you to override a method which is defined in the base class, in other words extend the implementation of the method in the derived class.

Differences between the new keyword and the override one can be found on MSDN here.

It is access modifiers (private, public, protected) that affects if you are able to call the base class method in the derived class or not.

Override and New Keywords (C# Programming)

Override/Virtual and New actually solve two different problems and have two different uses.

Here is an example of virtual/override. Where the base class and the child class both return the exact same type (void).

public abstract class ServiceBase 
{
public virtual void BeginTran()
{
base.BeginTran();
}
}

public sealed class SearchService : ServiceBase
{
public override void BeginTran()
{
base.BeginTran();
}
}

However, notice in your example using new you have this :

public sealed class SearchService : ServiceBase 
{
public new SearchService BeginTran()
{
base.BeginTran();
return this;
}
}

Your return type is now SearchService, not void. Because you have changed the return type, you can't "Override" the method, you need to use the new keyword to return a different type. This is called method "hiding".

There is also other side effects to this. Essentially the two methods are not connected, so polymorphism can often completely break and do unexpected things.

I would personally say that method hiding, or using the new keyword is a direct contradiction of the Liskov Principle which says that types can be replaced by subtypes without altering the program. In your case, that would not be true if you change the return type. Bit more info on that here : https://dotnetcoretutorials.com/2019/10/20/solid-in-c-liskov-principle/

In 15+ years of C# experience, I have used the new keyword maybe a handful of times, and almost always when trying to override a library method and bastardize the code.

C# - Why can't a virtual function override an abstract function?

A method that overrides its base method is implicitly virtual (unless specified otherwise with the sealed keyword), you don't need to specify it.

Sealed keyword in association with override

Sealing a method only makes sense if you override it.

What happens here is the following:

You are overriding a method from a base class (override) and tell the compiler that classes derived from your class are no longer allowed to override this method (sealed).

If the method is a new one declared by you in your class and you want to prevent derived classes from overriding it, simply don't declare it as virtual.

If the method is declared in a base class but is not overridable sealing it wouldn't make any sense, because it already can't be overriden.



Related Topics



Leave a reply



Submit