How is shadowing different from overriding in .NET?

Shadowing and Overriding are the concepts used during inheritance.




Purpose of each of them along with their differences is tabulated below:

Shadowing
Overriding
When base class and derived class have a method of same name but they are independent copies, then it is termed as shadowing. When derived class extends and overrides a method of base class, then it is termed as overriding.
Shadowing is achieved by using the new keyword in the derived class method signature. Here is an example:
class baseClass {
public void sample(){ }
}
class derivedClass : baseClass {
public new void sample() { }
}
Overriding is achieved by using the override keyword in the derived class method signature. Here is an example:
class baseClass {
public virtual void sample(){ }
}
class derivedClass : baseClass {
public override void sample() { }
}
Shadowing doesn't bother if the base class method is marked as virtual or not. In the above example, the method sample is not marked as virtual but it will work. Even if you mark the base class method as virtual and define your derived class method using new modifier, it will not throw any error. Consider the following example:
namespace Application1 {
public class baseClass {
public virtual void sample() { }
}
public class derivedClass:baseClass {
public new void sample() { }
}
}
This example is legal.

For a method to be overridden, base class method has to be marked as abstract or virtual or it should be an overridden method of a higher level base class in the hierarchy. Consider the following example:
namespace Application1 {
class baseClass {
public void sample() { }
}
class derivedClass:baseClass {
public override void sample() { }
}
}
During execution of this code, you will get the following error:
'Application1.derivedClass.sample()': cannot override inherited member 'Application1.baseClass.sample()'because it is not marked virtual, abstract, or override
If the base class method is defined as abstract, then the method has to be overridden in the derived class. Defining the same method with the same signature using new modifier will not work. Consider the following example:
namespace Application1{
abstract class baseClass {
public abstract void sample();
}
class derivedClass : baseClass {
public new void sample() { }
}
}
During execution, this code will end up in the following error:
'Application1.derivedClass' does not implement inherited abstract member 'Application1.baseClass.sample()'
If there is any abstract method in the base class, then it has to overridden in the derived class using override keyword. Consider the following example:
namespace Application1{
abstract class baseClass {
public abstract void sample();
}
class derivedClass : baseClass {
public override void sample() { }
}
}
This is legal and it will work.


The method sharing the same name in base class and derived class can have a different method signature since they are actually two different methods. Consider the following example:
namespace Application1 {
class baseClass {
public void sample(){ };
}
class derivedClass:baseClass {
public void sample(int data) { }
}
}
This example is perfectly legal.
The method sharing the same name in base class and derived class are actually the same method. Hence they should have the same signature. Consider the following example:
namespace Application1 {
class baseClass {
public virtual void sample(){ };
}
class derivedClass:baseClass {
public override void
sample(int data) { }
}
}
This is not permissible and you will get the following error:
'Application1.derivedClass.sample(int)': no suitable method found to override
When you don't explicitly specify new or override keywords in your derived class method which shares the same name as your base class method, then it will implicitly have new modifier. A method will not be overridden implicitly. You have to explicitly override a method using override keyword.

_______________________________________________________________________

Add to My Yahoo!


FREE Subscription

Subscribe to our mailing list and receive new articles
through email. Keep yourself updated with latest
developments in the industry.

Name:
Email:

Note : We never rent, trade, or sell my email lists to
anyone. We assure that your privacy is respected
and protected.

Visit .NET Programming Tutorial Homepage

______________________________________________________

Recommended Resource

| How do you prevent a class from overriding in .NET? | How are classes related to objects in .NET Application | How are Delegates different from Events in .NET? | How are system exceptions different from application exceptions in .NET? | How are Value Types different from Reference Types in .NET? | How can a finalize method be suppressed in .NET? | How can you call Stored Procedure in ADO.NET? | How can you force Dispose method to be called automatically in .NET? | How do you call a Base Class Constructor from Derived Class Constructor in .NET? | How do you connect your VB.NET application to SQL Server? | How do you implement Cloning in .NET? | How do you implement Façade Design Pattern in .NET? | How do you implement MVC Pattern in ASP.NET? | How do you install .NET Assembly in GAC? | How is shadowing different from overriding in .NET? | How to prevent a particular .NET DLL from being decompiled? | Illustrate Delay Signing Process of an Assembly in .NET? | What are Reference Types in .NET? | What are the advantages of C#? | What are the advantages of VB.NET? | What are the differences between Namespace and Assembly in .NET? | What are the similar features between class and structure in .NET? | What are Value Types in .NET? | What do you mean by mixed mode authentication in .NET? | What do you mean by Satellite Assembly in .NET? | What do you mean by shadowing in .NET? | What is CTS in .NET? | What is ILDASM in .NET? | What is Managed Code in .NET? | What is Manifest in .NET? | What is MSIL in .NET Framework? | What is the importance of finalize method in .NET? | What is the need for Visitor Pattern in C#? | What is the purpose of bindingRedirect tag in web.config file of .NET? | What is the purpose of CodeDom in .NET? | What is the purpose of dispose method in .NET? | What is the purpose of Ngen.exe in .NET? | What is the purpose of Strong Name in COM Components of .NET? | What is the purpose of virtual keyword in .NET? | What Object Oriented Principles can be incorporated in .NET Application? |


| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2017 - All Rights Reserved.