What do you mean by shadowing in .NET?

Assume that you have a class that derives from the base class and redefines a method in the base class. Then what process is happening? You will generally assume it as overriding meaning that derived class method overrides base class method and redefines the method.




Even when it redefines, the derived class method should have the same signature as base class method and the derived class method should include the keyword override in its method signature.

What if you don’t explicitly specify the override keyword in your derived class method? Even then does it mean as overriding. No, if you don’t explicitly use override then the derived class method is shadowing base class method. This means that derived class is creating a new method that is no way related to the base class method. But the derived class method completely shadows/hides base class method. Unlike overriding, members sharing same name in derived class can have a totally different signature since it is a new member.

Shadowing happens by default when two members share the same name but override keyword is not mentioned. However you can explicitly perform shadowing using a keyword.

The keyword differs between languages:
• In C#, shadowing is done by using new modifier
• In VB.NET, shadowing is done using shadows keyword

Given below are examples to demonstrate shadowing in both the languages.

Shadowing in C#:
In the following example, method displayMsg of baseClass and data member called member are shadowed by the derived class members using the new modifier:
class baseClass {
public int member;
baseClass(int data) {
member = data;
}
public void displayMsg() {
Console.WriteLine(“In baseClass..”);
}
}
class derivedClass:baseClass {
new public string member;
public derivedClass(string data) {
member = data;
}
new public void displayMsg(string msg) {
Console.WriteLine(“In derivedClass with msg:”+msg);
}
}
class testClass {
baseClass bObj = new baseClass(100);
Console.WriteLine(“Member value of bObj is:” + bObj.member);
bObj.displayMsg();
derivedClass dObj = new derivedClass(“Hello”);
Console.WriteLine(“Member value of dObj is:” + dObj.member);
dObj.displayMsg(“Good Day!”);
}
Output of this code will be:
Member value of bObj is: 100
In baseClass..
Member value of dObj is: Hello
In derivedClass with msg: Good Day!

In this example, derived class has completely shadowed member and displayMsg of baseClass. Note that the signature of member and displayMsg is different in derivedClass when compared to baseClass. This is acceptable in case of shadowing. But in overriding, it will throw error.

Shadowing in VB.NET:
In VB.NET, shadowing is done with the help of shadows keyword as demonstrated in the example shown below:
Public Class baseClass
Public member As Integer = 100
End Class

Public Class derivedClass
Inherits baseClass
Public Shadows member As String = “Hello”
End Class

Module testModule
Sub Main()
Dim bObj as New baseClass
Dim dObj as New derivedClass
MessageBox.Show(bObj.member)
MessageBox.Show(dObj.member)
End Sub
End Module

The MessageBox will display 100 first and then Hello.

Invoking Base Class Member Using Derived Class Object:
So far in the discussion, it was told that the baseClass member is completely hidden in derivedClass. Does that mean you cannot access the baseClass member in derivedClass? No. You still have an option using references. Given below is the modified code of testClass containing code to access baseClass member using derivedClass object:
Module testModule
Sub Main()
Dim dObj As New derivedClass
Dim refBaseClass As baseClass = dObj
MessageBox.Show(&refBaseClass.member)
End Sub
End Module

Now to your surprise, you will get the output as 100. This is because you create a reference to baseClass which is assigned with derivedClass object. Whatever is the assigned object, the reference takes priority and the baseClass member gets displayed. In this case, shadowing is suppressed.

_______________________________________________________________________

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 - 2014 - All Rights Reserved.