Differentiate between Shadowing and Overriding

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.

Comparision of Shadowing, Hiding and Overriding
  1. The access level, signature and the return type can only be changed when you are shadowing with VB.NET. Hiding and overriding demands that these parameters are same.
  2. The difference lies when you call the derived class object with a base class variable. In case 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.

There are two main Differences between Shadowing and Overriding
  1. Overriding redefines only the implementation but shadowing redefines the whole Element.
  2. In Overriding (VB.NET), the Derived class can refer the Base class using Me keyword but in shadowing we can access it using MyBase.

ShadowingIt hides a base class member in the derived class by using the new keyword. It is used to explicitly hide a member inherited from base class. new and override both cannot be used for the same member.

Example

class Employee
{
double m_dblBasicSalary;

public Employee(double dblBasicSalary)
{
m_dblBasicSalary = dblBasicSalary;
}

public virtual double CalculateSalary()
{
return m_dblBasicSalary;
}
}

class SalesPerson : Employee
{
double m_dblBasicSalary, m_dblSales, m_dblComm;

public SalesPerson(double dblBasicSalary, double dblSales, double dblComm):base(dblBasicSalary)
{
m_dblBasicSalary = dblBasicSalary;
m_dblSales = dblSales;
m_dblComm = dblComm;
}

public new double CalculateSalary()
{
return m_dblBasicSalary + (m_dblSales * m_dblComm);
}
}

class Program
{
static void Main(string[] args)
{
Employee oSalesPerson = new SalesPerson(1500, 20, 5);
double dblSalary = oSalesPerson.CalculateSalary();
Console.WriteLine(dblSalary);
Console.ReadKey();
}
}

Overriding: In overriding, methods have same names, same signatures, same return types but in different classes. C# uses virtual and override keyword for method overriding.Example

class Employee
{
double m_dblBasicSalary;

public Employee(double dblBasicSalary)
{
m_dblBasicSalary = dblBasicSalary;
}

public virtual double CalculateSalary()
{
return m_dblBasicSalary;
}
}

class SalesPerson : Employee
{
double m_dblBasicSalary, m_dblSales, m_dblComm;

public SalesPerson(double dblBasicSalary, double dblSales, double dblComm):base(dblBasicSalary)
{
m_dblBasicSalary = dblBasicSalary;
m_dblSales = dblSales;
m_dblComm = dblComm;
}

public override double CalculateSalary()
{
return m_dblBasicSalary + (m_dblSales * m_dblComm);
}
}

class Program
{
static void Main(string[] args)
{
Employee oSalesPerson = new SalesPerson(1500, 20, 5);
double dblSalary = oSalesPerson.CalculateSalary();
Console.WriteLine(dblSalary);
Console.ReadKey();
}
}

When you call the derived class object with a base class variable, in the case 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.

1 comment:

  1. Nice tutorial on this topic.
    I will be happy if you visit my blog IT Help Zone too with similar topics but for beginners :)

    ReplyDelete