Tech Point Fundamentals

Tuesday, November 24, 2020

Shadowing | Method Hiding in C#

Shadowing | Method Hiding in C#

 C# not only supports method overriding but method hiding also. In C# you can hide the methods of the base class from the derived class, this concept is known as Method Hiding or Method Shadowing. The term "Shadowing" is normally used in VB.Net which is known as "Hiding" in C#.Net. Today, we will learn about shadowing in C#.


Shadowing is a VB.Net concept while hiding is a C#.Net concept. Both term "Shadowing" and "Hiding" are the same in C#. When you say "shadow" you're usually talking about scope while when you say "hiding" you're usually talking about inheritance

When two-member uses the same name, one of them can hide, or shadow, the other one. In such a situation, the shadowed element is not available for reference; instead, when your code uses that member name, the compiler resolves it to the shadowing element. In shadowing the parent class element is completely replaced by the child class in all terms. 

Shadowing (Hiding)

By shadowing the child class can create its own version of the base class method or property. A method of the base class is available to the child class with a new definition or implementation without using the override keyword

The "new" keyword is used to perform shadowing, but if it is not used the compiler generates a warning. The compiler hides the method or property of the base class. 

Shadowing Fundamental Points

    1. Overriding redefines only the implementation not signature but shadowing redefines the whole element.
    2. The method signature, access level, and return type of the shadowed member can be completely different from the base class member.
    3. The shadowed element can be inherited further in the derived class. The shadowed element will still be hidden in the derived class.
    4. If the shadowing element is inaccessible in a further derived class, shadowing is not inherited i.e. the derived class inherits the original element instead of the shadowing element.
    5. A shadowed method of the base class is available to the child class without the use of the “overriding” keyword.
    6. If you do not specify either shadows or overrides, the compiler issues a warning message to help you be sure which kind of redefinition you want to use.
    7. Base class hidden members can be accessed only either by the "base" keyword or by creating a base class reference variable or by casting derived class object to the base class type.
    8. If an element is simply hidden then the implementation to call is based on the compile-time type of the argument “this” instead of run time like overriding.
    9. If a base class method is not overridden in the derived class, it is hiding the base class method.

Shadowing Fundamental Rules

    1. Both virtual and non-virtual members can be shadowed.
    2. The "new" keyword is optional for hiding. If you do not use the new keyword the compiler generates the warning.
    3. The shadowing and method overriding both can be achieved together by using the virtual modifier with the new keyword i.e we can hide the base class normal method as virtual.
    4. We can hide base class virtual methods by using the "new" keyword. ie we can use the new keyword with virtual keywords together. 
    5. We cannot use the new and override keywords together because the two modifiers have mutually exclusive meanings.
    6. The new implementation will only be available in the child-class, not to the base class.
    7. In shadowing a base class reference variable pointing to a child class object will invoke the hidden method in the base class i.e. base class members will be called not child class.
    8. A base class member can be shadowed to private only if the shadowing definition is not virtual, because a virtual member cannot be private.
    9. If a shadowing member is defined as private then, it will not be inherited further.  In such a case, shadowing is defeated and the compiler resolves any reference to the same element, it would have if there had been no shadowing.

Why Shadowing

The main purpose of shadowing is to protect the definition of the class members. The base class might undergo a change that creates an element or member with the same name as the one you have already defined in the child class. If this happens then which version should be called.

Shadowing can be used when:
    1. If we cannot override the base class method as it is not virtual in the base class. 
    2. If the child class wants to implement some methods of the base class without disturbing the base class.
    3. If the base class is available in a library that cannot be changed even if we want to do so intentionally.



Types of Shadowing

An element can shadow another element in two different ways:

1. By Scope (Without Inheritance)

If a module, class, or structure have members with the same name but in different region or scope, then it is known as shadowing by scope.

The shadowing element can be declared inside a subregion of the region containing the shadowed element, in which case the shadowing is accomplished through the scope. 

When two elements are declared in this manner and the code refers to the name they share, the element with the narrower scope shadows the other element.

2. By Inheritance

If a derived class redefines a member element inherited from a base class, the redefining element shadows the original element of the base class. You can shadow any type of declared element, or set of overloaded elements, with any other type.

Accessing Shadowed Members of Base Class

In C# you can also access the hidden method of the base class in the derived class using three different ways:

i) By using the "base" keyword     

ii) By casting the Derived Class object type to the Base Class type

iii) By creating a Base Class reference variable    
      BaseClass _baseClassRef = new DerivedClass();

How Shadowing is different from Overloading

Shadowing is not the same as overloading in C#. If you shadow the same base class element with more than one element in your derived class, the shadowing elements become overloaded versions of that element.

    1. Only a method or operator can be overloaded not properties or variables on the other any element type can be shadowed.
    2. For overloading the signature must be different ( no of parameters, type of parameters, order of parameters, etc) on the other hand it is not mandatory for shadowing.

Difference between Shadowing and Overriding

    1. Shadowing is used to protect against subsequent base class modification, while overriding does polymorphism by defining a different implementation.
    2. Shadowing can redefine the entire signature, on the other hand, overriding can redefines only the implementation of a method, not the signature.
    3. For overriding, the base class member must be declared as virtual while it is not mandatory for shadowing.
    4. In shadowing, we can change the access modifier, while we cannot change the access modifier in overriding.
    5. You cannot override a method with a property (or variable), or vice-versa, but you can shadow.
    6. The "override" keyword cannot be used with the "virtual" modifier while the "new" keyword can be used with the "virtual" modifier.
    7. In the case of shadowing, the base class cannot access the shadowed members in the derived class, while in the case of overriding the base class can access the overridden members in the derived class.
    8. There is no control of a base class on shadowing i.e. a base class cannot enforce or prohibit shadowing, but it has control on overriding.
    9. In overriding, a base class reference variable pointing to a child class object will invoke the overridden member in the derived class, not the base class. While in the case of shadowing it will invoke the base class members, not the derived class shadowed members.

Please read more about the method overriding here.

Live Demo


Shadowing is very confusing, and also very dangerous if not used properly. Shadowing concept breaks the rule of the SOLID principle as well as Design Pattern Techniques.  Shadowing leads to bad coding practice so use shadowing only when required. If you can solve the problem by overriding, then always go with that instead of shadowing.

No comments:

Post a Comment

Please do not enter any HTML. JavaScript or spam link in the comment box.