Difference Between New and Override

Difference between new and override

The override modifier may be used on
virtual methods and must be used on
abstract methods. This indicates for
the compiler to use the last defined
implementation of a method. Even if
the method is called on a reference to
the base class it will use the
implementation overriding it.

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

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

Base b = new Derived();
b.DoIt(); // Calls Derived.DoIt

will call Derived.DoIt if that overrides Base.DoIt.

The new modifier instructs the
compiler to use your child class implementation
instead of the parent class
implementation. Any code that is not
referencing your class but the parent
class will use the parent class
implementation.

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

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

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

b.DoIt(); // Calls Base.DoIt
d.DoIt(); // Calls Derived.DoIt

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

Source: Microsoft blog

What is the difference between the override and new keywords in C#?

The following page summarizes your question very nicely.

Knowing When to Use Override and New Keywords

Summary

Override: 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.

New: 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.

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).

Override: used with virtual/abstract/override type of method in base class

New: when base class has not declared method as virtual/abstract/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.

C# - Keyword usage virtual+override vs. new

The "new" keyword doesn't override, it signifies a new method that has nothing to do with the base class method.

public class Foo
{
public bool DoSomething() { return false; }
}

public class Bar : Foo
{
public new bool DoSomething() { return true; }
}

public class Test
{
public static void Main ()
{
Foo test = new Bar ();
Console.WriteLine (test.DoSomething ());
}
}

This prints false, if you used override it would have printed true.

(Base code taken from Joseph Daigle)

So, if you are doing real polymorphism you SHOULD ALWAYS OVERRIDE. The only place where you need to use "new" is when the method is not related in any way to the base class version.

When to use new instead of override C#

The new keyword is the right way to go when you want to hide a base implementation and you don't care that you won't be able to call your method when treating your object polymorphically.

Let me clarify. Consider the following:

public class Base
{
public void DoSomething()
{
Console.WriteLine("Base");
}
}

public class Child : Base
{
public new void DoSomething()
{
Console.WriteLine("Child");
}
}

From time to time, it is beneficial to hide the base class' functionality in the child hence the use of the new keyword. The problem is that some developers do this blindly which will lead to the following side effect (hence the 'bugs' you mention):

Base instance = new Child();
instance.DoSomething(); // may expect "Child" but writes "Base"

Difference between C# new and override keyword on property

The difference is summarized on the Microsoft page Knowing When to Use Override and New Keywords (C# Programming Guide).

Review the section of code indicated there. It is also listed below:

public static void TestCars2()
{
Car[] cars = new Car[3];
cars[0] = new Car();
cars[1] = new ConvertibleCar();
cars[2] = new Minivan();
}

foreach (Car vehicle in cars)
{
System.Console.WriteLine("Car object: " + vehicle.GetType());
vehicle.DescribeCar();
System.Console.WriteLine("----------");
}

The same principle is getting applied to your source. The .NET framework treats all controls on the page as a collection of base Control classes. Control.Visible is a virtual property.

This means: when the ASP.NET framework invokes Control.Visible for all controls, the only Control.Visible property that is invoked from your source is the one marked as override, which is what people typically think of with standard polymorphism. Otherwise, if you declare your property as new, when the ASP.NET framework invokes Control.Visible, your property is never called.

Thus the override results in a stack overflow exception since you are then calling your panel.Visible, which is recursively calling itself many times.

override ToString() with override and new keyword

It makes a difference when you do this:

object a = new A(); // notice the type of the variable here!
object b = new B();
Console.WriteLine(a.ToString());
Console.WriteLine(b.ToString());

a.ToString() will not call your implementation of ToString and will instead call object.ToString, which returns the fully qualified type name of the object. b.ToString() will call your implementation.

What you did in B is called overriding. What you did in A is called hiding. Hiding loses its effect when the compile time type of a variable is not that type anymore. Your implementation of ToString will only be called when the compile time type is A.

Learn more here.



Related Topics



Leave a reply



Submit