Method Hiding & Method Shadow in OOPs

Method Hiding / Method Shadowing is a feature of C# that enables you to hide base class methods from derived classes.

Method Hiding & Method Shadow in OOPs
Method Hiding, Method Shadowing, OOPs, Programming, thetechfoyer, thetechfoyer.com

This Article helps to understand the concept of Method Hiding & Method Shadow in OOPs

Then main purpose of shadowing is to protect the definition of your class memebers.
In method hiding, you can hide the implementation of the methods of a base class from the derived class.



Shadowing Through Scope
The method of the parent class is available to the child class without using the override keyword in shadowing. The child class has its own version of the same function.
Use the new keyword to perform shadowing and to create the own version of the base class function.

class Demo {
  public class Parent {
     public string Display() {
        return "Parent Class!";
     }
  }
 
  public class Child : Parent {
     public new string Display() {
        return "Child Class!";
     }
  }

  static void Main(String[] args) {
     Child child = new Child();
     Console.WriteLine(child.Display());
  }
}

//Output: Child Class!



Shadowing through Inheritance / Overriding
Under overriding, you can define a behavior that's specific to the subclass type, which means a subclass can implement a parent class method based on its requirement.

public static void Main(string[] args)
{
        Rectangle r = new Rectangle(10, 7);
    double a = r.area();
    Console.WriteLine("Area: {0}",a);
    Console.ReadKey();
}

abstract class Shape {
    public abstract int area();
}

class Rectangle: Shape {
    private int length;
    private int width;

    public Rectangle( int a = 0, int b = 0) {
        length = a;
        width = b;
    }

    public override int area () {
        Console.WriteLine("Rectangle class area :");
        return (width * length);
    }
}

// Rectangle class area :
// Area: 70



Call the hidden method of the base class in the derived class

In method hiding, you can also call the hidden method of the base class in the derived class using three different ways and the ways are:
1. By using the base keyword you can call the hidden method of the base class in your derived class

public static void Main(string[] args)
{
        // Creating the object of the derived class
        Member obj = new Member();  
        // Access the method of derived class
        obj.member();
}
   
public class Family {
    public void member()
    {
        Console.WriteLine("Total number of family members: 3");
    }
}
 
// Derived Class
public class Member : Family {
    public new void member()
    {
        // Calling the hidden method of the
        // base class in a derived class
        // Using base keyword
        base.member();
        Console.WriteLine("Name: Deepak, Age: 45 \nName: Kumar,"+
                              " Age: 25 \nName: Shyam, Age: 20");
    }
  }

/*
  Output:
  Total number of family members: 3
  Name: Deepak, Age: 45
  Name: Kumar, Age: 25
  Name: Shyam, Age: 20
*/



2. By casting the derived class type to base class type

public class Family {
  public void member()
  {
      Console.WriteLine("Total number of family members: 2");
  }
}

// Derived Class
public class Member : Family {      
  public new void member() {  
      Console.WriteLine("Name: Deepak, Age: 45 \nName: Kumar,"+
                            " Age: 25 \nName: Shyam, Age: 20");
  }
}


// Main method
public static void Main(string[] args)
{
  // Creating the object of the derived class
  Member obj = new Member();

  // Invoking the hidden method
  // By type casting
  ((Family)obj).member();
}

//Output
//Total number of family members: 2



3. Use the parent class reference variable for calling the hidden method.

// Base Class
public class Family {
 
  public void member()
  {
      Console.WriteLine("Total number of family members: 2");
  }
}

// Derived Class
public class Member : Family {  
  public new void member() {  
      Console.WriteLine("Name: Deepak, Age: 45 \nName: Kumar,"+
                            " Age: 25 \nName: Shyam, Age: 20");
  }
}


// Main method
public static void Main(string[] args)
{

  // Invoking the hidden method
  Family obj = new Member();
  obj.member();
}

//Output: Total number of family members: 2