New Message on dotNET User Group Hyd

Article : - Overriding, Shadowing and Hiding in NET

Reply
  Reply to Sender   Recommend Message 1 in Discussion
From: Nasha

Hey Group,
   
Howz it goin ? I am back after a weeks break. Let me start by wishing all of you A Very Happy & Prosperous NewYear !!!!

Today we will discuss what is overriding , shadowing and hiding ? .

Most of us I guess are familiar with overridering from C++ or Java. Overriding as the term says is to override  the base class  implementation  or provide a new  implementation in the derived class. We can override members in the base class in VB.Net using  the keyword Overrides and in C# using the key word override. To override a base class member in VB.Net it should be marked as  Overridable and in C# they should be marked with virtual. Method signature of the overiddden methods should be same as the base  class methods.

Shadowing :- This is a VB.Net Concept by which you can provide a new implementation for the base class member without overriding  the member. You can shadow a base class member in the derived class by using the keyword "Shadows". The method  signature,access level and return type of the shadowed member can be completely different than the base class member.

Hiding : - This is a C# Concept by which you can provide a new implementation for the base class member without overriding the  member. You can hide a base class member in the derived class by using the keyword "new". The method signature,access level and  return type of the hidden member has to be same as the base class member.

Comparing the three :-

1) The access level , signature and the return type can only be changed when you are shadowing with VB.NET. Hiding and overriding  demands the these parameters as same.

2) The difference lies when you call the derived class object with a base class variable.In class of overriding although you assign a  derived class object to base class variable it will call the derived class function. In case of shadowing or hiding the base class function  will be called.

Let us create a sample application and this check out :-

1) Create a new solution and three projects to it. One VB.Net Class Library, another C# Class Library and C# Console Application as  a Client Application.

2) Name your VB.Net Class Library as Shadowing , C# library as Hiding and Your console application as Client Application.

3) In Shadowing add a class file called Shadow.vb and add the following classes to it.

Public Class BaseShadow

    Public Overridable Function OverrideMe() As String
        Return "Base Class Override Me"
    End Function

    Public Function ShadowMe() As String
        Return "Base Class Shadow Me"
    End Function

End Class


Public Class DerivedShadow
    Inherits BaseShadow

    Public Overrides Function OverrideMe() As String
        Return "Derived Class Override Me"
    End Function

    Public Shadows Function ShadowMe() As String
        Return "Derived Class Shadow Me"
    End Function

End Class

4) In Hiding add class file called Hide.cs and add the following classes to it.

using System;

namespace Hiding
{
        public class BaseHide
        {
                public BaseHide()
                {

                }

                public virtual string OverrideMe()
                {
                        return "Base Class Override Me";
                }

                public string HideMe()
                {
                        return "Base Class Hide Me";
                }

        }

        public class DerivedHide:BaseHide
        {
                public DerivedHide()
                {

                }
                public override string OverrideMe()
                {
                        return "Derived Class Override Me";
                }

                public new string HideMe()
                {
                        return "Derived Class Hide Me";
                }

        }
}

5) Build both the Class Libs and add their refernces to the client console application.

6) Add the following code in the Main of the console Application.

using System;
using Hiding;
using Shadowing;
       
namespace Client_Application
{
        /// <summary>
        /// Summary description for Class1.
        /// </summary>
        class Client
        {
                /// <summary>
                /// The main entry point for the application.
                /// </summary>
                [STAThread]
                static void Main(string[] args)
                {

                        Console.WriteLine("// ****  Overriding **** //");
                        // ****  Overriding **** //

                        Shadowing.DerivedShadow derivedShadowOverrideObject = new Shadowing.DerivedShadow();
                        Console.WriteLine("Accessing thru derived class " + derivedShadowOverrideObject.OverrideMe());

                        Shadowing.BaseShadow baseShadowOverrideObject = new Shadowing.DerivedShadow();
                        Console.WriteLine("Accessing thru base class " + baseShadowOverrideObject.OverrideMe());

                        Hiding.DerivedHide derivedHideOverrideObject = new Hiding.DerivedHide();
                        Console.WriteLine("Accessing thru derived class " + derivedHideOverrideObject.OverrideMe());

                        Hiding.BaseHide baseHideOverrideObject = new Hiding.DerivedHide();
                        Console.WriteLine("Accessing thru base class " + baseHideOverrideObject.OverrideMe());


                        // ****  Hiding **** //

                        Console.WriteLine("// ****  Hiding **** //");
               
                        Hiding.DerivedHide derivedHideObject = new Hiding.DerivedHide();
                        Console.WriteLine("Accessing thru derived class " + derivedHideObject.HideMe());
                       
                        Hiding.BaseHide baseHideObject = new Hiding.DerivedHide();
                        Console.WriteLine("Accessing thru base class " + baseHideObject.HideMe());

                        // ****  Shadowing **** //

                        Console.WriteLine("// ****  Shadowing **** //");

                        Shadowing.DerivedShadow derivedShadowObject = new Shadowing.DerivedShadow();
                        Console.WriteLine("Accessing thru derived class " + derivedShadowObject.ShadowMe());

                        Shadowing.BaseShadow baseShadowObject = new Shadowing.DerivedShadow();
                        Console.WriteLine("Accessing thru base class " + baseShadowObject.ShadowMe());

                        Console.ReadLine();
                }
        }
}

7) Execute the code and checkout the differences. You can even go ahead and change the method signature, accesslevel and return type for Shadowing and check it out.
 

-- Please post your queries and comments for my articles in the user group for the benefit of all. I hope this step from my end is helpful to all of us. You can find all articles written by me at my personal Blog http://spaces.msn.com/members/nasha .

Regards,

Namratha (Nasha)
<o:p></o:p>

 <o:p></o:p>


View other groups in this category.

Click Here!
Also on MSN:
Start Chatting | Listen to Music | House & Home | Try Online Dating | Daily Horoscopes

To stop getting this e-mail, or change how often it arrives, go to your E-mail Settings.

Need help? If you've forgotten your password, please go to Passport Member Services.
For other questions or feedback, go to our Contact Us page.

If you do not want to receive future e-mail from this MSN group, or if you received this message by mistake, please click the "Remove" link below. On the pre-addressed e-mail message that opens, simply click "Send". Your e-mail address will be deleted from this group's mailing list.
Remove my e-mail address from dotNET User Group Hyd.

Reply via email to