C# Interview Questions and Answers
<< Previous Question Next Question >>
 Question: 2676 Page Views: 

Explain the difference between override and new in C#?



Posted By: Avi Date: 30 November 2009 04:45:28 AM
 Answer:

When a virtual method is called on an object reference, the actual type of that object 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 b = new Derived();
b.SomeMethod();

It 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();

It will first call Base.SomeOtherMethod (line 3), then Derived.SomeOtherMethod (line 4). 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.


Posted By: eTechPlanet


Date: 30 November 2009 04:45:28 AM

The differences between the two C# keywords is due to polymorphism. When a virtual method is called on a reference, the actual type of the object to which the reference refers is used to determine which method implementation should be used. When a method of a base class is overridden in a derived class (subclass), the version defined in the derived class is used. This is so even should the calling application be unaware that the object is an instance of the derived class. The following example illustrates the override keyword: public class BaseClass { public virtual void AMethod() { } } public class DerivedClass : BaseClass { public override void AMethod() { } } // ... BaseClass baseClass = new DerivedClass(); baseClass.AMethod(); In the preceding example, DerivedClass.AMethod will be called; because, it overrides BaseClass.AMethod. If the new keyword were used instead of the override keyword, then the method in the derived class would not override the base class method, but would hide it instead. The following is an example illustrating the new keyword: public class BaseClass { public virtual void AMethod() { } } public class DerivedClass : BaseClass { public new void AMethod() { } } // ... BaseClass baseClass = new DerivedClass(); baseClass.AMethod(); DerivedClass derivedClass = new DerivedClass(); derivedClass.AMethod(); The preceding example will call BaseClass.AMethod first, followed by DerivedClass.AMethod. Effectively, the two methods are unrelated except for the fact that they have the same name. If neither the new nor overrides keyword is specified, the result is the same as if the new were specified. However, a compiler warning will be generated to ensure that the programmer is aware that a method in the base class is being hidden. Term papers


Posted By: Johndecruse


Date: 10 February 2010 02:33:31 AM
Post a better Answer if you have
 
(Will show your Gravatar icon)  
  Country flag

Loading
Enter the text as shown in the image xAvAZb
Related Questions
C# : How do you prevent property from being serialized?

How do you prevent property from being serialized?

by marking it with [NonSerialized()] attribute
Category: C# Date: 10/13/2010 9:10:46 AM
C# : What is meant by MulticastDeligate?

What is meant by MulticastDeligate?

MulticastDeligate is a special class.It is a deligate that can have more than one element in its inv....
Category: C# Date: 10/1/2010 11:54:31 AM
C# : Write a code to convert string to integer (int) data type.

Write a code to convert string to integer (int) data type.

class conver { public static void Main() { String a = "2000"; int x = Convert.ToInt32(a)....
Category: C# Date: 10/1/2010 11:53:31 AM
C# : Is it possible to override a private virtual method?

Is it possible to override a private virtual method?

The naswer is no. Also Virtual or abstract methods can not have private as access modifier. Also pri....
Category: C# Date: 10/1/2010 11:52:31 AM
C# : Specify the ways which help in breking out the block or loop when working with the nested loops?

Specify the ways which help in breking out the block or loop when working with the nested loops?

There are several ways to break out of a nested loop:- 1. The simplest and the easiest way is to us....
Category: C# Date: 10/1/2010 11:51:31 AM