1.

What is the use of the method "hiding" in inheritance?

Answer»

Method hiding or Method shadowing is used to hide the implementation of the base class method under the shadow of the child class method, with the help of a new keyword.

This is done to clear the ambiguity when the child class has a method with the same name as that of the base class and hence helps in abstraction.

// C# program to illustrate the
// concept of method hiding
using System;
// Base Class
public class Parent {
public void member()
{
Console.WriteLine("Parent method");
}
}
// Derived Class
public class Child : Parent {
// Reimplement the method of the base class
// Using new keyword
// It hides the method of the base class
public new void member()
{
Console.WriteLine("Child method");
}
}
// Driver Class
class DriverClass {
// Main method
static public void Main()
{
// Creating the object of the derived class
Child obj = new Child();
// Access the method of derived class
obj.member();
}
}


Discussion

No Comment Found