As we already know about polymorphism and method overriding in C#. C# also provides a concept to hide the methods of the base class from derived class, this concept is known as Method Hiding. It is also known as Method Shadowing. In method hiding, you can hide the implementation of the methods of a base class from the derived class using the new keyword. Or in other words, in method hiding, you can redefine the method of the base class in the derived class by using the new keyword.
Example:
using System;
public class My_Family {
public void member()
{
Console.WriteLine( "Total number of family members: 3" );
}
}
public class My_Member : My_Family {
public new void member()
{
Console.WriteLine( "Name: Rakesh, Age: 40 \nName: Somya, " +
"Age: 39 \nName: Rohan, Age: 20 " );
}
}
class GFG {
static public void Main()
{
My_Member obj = new My_Member();
obj.member();
}
}
|
Output:
Name: Rakesh, Age: 40
Name: Somya, Age: 39
Name: Rohan, Age: 20
Explanation: In the above example, My_Family is the base class and My_Member is a derived class. In both the classes we have the same name method, i.e. member() method. But in the derived class, the member() method is declared with the new keyword. When this method is called, it prints the name and the age of the family members not the total number of family members. Which means when we call the member() method with the help of the derived class object, it hides the same name method present in the base class due to the presence of the new keyword.
Now we will see what will happen if we do not use the new keyword to hide the method of a base class from the derived class. As shown in the below example, when we do not use new keyword compiler will give run the code without giving an error, but it will give warning that if you want to hide a method then uses the new keyword.
Example:
using System;
public class My_Family {
public void member()
{
Console.WriteLine( "Total number of family members: 3" );
}
}
public class My_Member : My_Family {
public void member()
{
Console.WriteLine( "Name: Rakesh, Age: 40 \nName: Somya, " +
"Age: 39 \nName: Rohan, Age: 20 " );
}
}
class GFG {
static public void Main()
{
My_Member obj = new My_Member();
obj.member();
}
}
|
Warning:
prog.cs(18,14): warning CS0108: `My_Member.member()’ hides inherited member `My_Family.member()’. Use the new keyword if hiding was intended
prog.cs(9,14): (Location of the symbol related to previous warning)
Output:
Name: Rakesh, Age: 40
Name: Somya, Age: 39
Name: Rohan, Age: 20
How to call a hidden method?
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:
- By using the base keyword you can call the hidden method of the base class in your derived class shown in the below example:
Example:
using System;
public class My_Family {
public void member()
{
Console.WriteLine( "Total number of family members: 3" );
}
}
public class My_Member : My_Family {
public new void member()
{
base .member();
Console.WriteLine( "Name: Rakesh, Age: 40 \nName: Somya," +
" Age: 39 \nName: Rohan, Age: 20" );
}
}
class GFG {
static public void Main()
{
My_Member obj = new My_Member();
obj.member();
}
}
|
Output:
Total number of family members: 3
Name: Rakesh, Age: 40
Name: Somya, Age: 39
Name: Rohan, Age: 20
- By casting the derived class type to base class type you can invoke the hidden method. As shown in the below example. We know that in inheritance the derived class has all the capabilities of the base class so we can easily cast the object of a derived class into base class type.
Example:
using System;
public class My_Family {
public void member()
{
Console.WriteLine( "Total number of family members: 2" );
}
}
public class My_Member : My_Family {
public new void member() {
Console.WriteLine( "Name: Rakesh, Age: 40 " +
"\nName: Somya, Age: 39" );
}
}
class GFG {
static public void Main()
{
My_Member obj = new My_Member();
((My_Family)obj).member();
}
}
|
Output:
Total number of family members: 2
- Instead of using derived class reference variable we use the parent class reference variable for calling the hidden method. It is similar to the above way of calling a hidden method. Here we also type case the object of the derived class in a different syntax.
Note: If you try to invoke a hidden method using below syntax,
My_Member obj = new My_Family();
Here, the compiler will give an error because the object of My_Family class cannot fulfill the duties of My_Member class.
Example:
using System;
public class My_Family {
public void member()
{
Console.WriteLine( "Total number of family members: 2" );
}
}
public class My_Member : My_Family {
public new void member() {
Console.WriteLine( "Name: Rakesh, Age: 40 " +
"\nName: Somya, Age: 39" );
}
}
class GFG {
static public void Main()
{
My_Family obj = new My_Member();
obj.member();
}
}
|
Output:
Total number of family members: 2
Similar Reads
Main Method in C#
C# applications have an entry point called Main Method. It is the first method which gets invoked whenever an application started and it is present in every C# executable file. The application may be Console Application or Windows Application. The most common entry point of a C# program is static vo
5 min read
Extension Method in C#
In C#, the extension method concept allows you to add new methods in the existing class or in the structure without modifying the source code of the original type and you do not require any kind of special permission from the original type and there is no need to re-compile the original type. It is
5 min read
Decimal.Divide() Method in C#
This method is used to divide the two specified decimal values. Syntax: public static decimal Divide (decimal a1, decimal a2); Parameters: a1: This parameter specifies the dividend. a2: This parameter specifies the divisor. Return Value: The result of dividing a1 by a2. Exceptions: DivideByZeroExcep
2 min read
Decimal.Add() Method in C#
This method is used to add two specified decimal values. Syntax: public static decimal Add (decimal a1, decimal a2); Parameters: a1: This parameter specifies the first value to add. a2: This parameter specifies the second value to add. Return Value: Decimal sum of a1 & a2. Exceptions: This metho
2 min read
DateTime.Add() Method in C#
This method is used to return a new DateTime that adds the value of the specified TimeSpan to the value of this instance. Syntax: public DateTime Add (TimeSpan value); Here, value is a positive or negative time interval. Return Value: This method returns an object whose value is the sum of the date
2 min read
Decimal.Floor() Method in C#
This method is used to round the decimal to the closest integer toward negative infinity. Syntax: public static decimal Floor (decimal d); Parameter: d: This parameter specifies the decimal which will be rounded off. Return Value: If d has a fractional part, the next whole Decimal number toward nega
2 min read
DateTime.DaysInMonth() Method in C#
This method returns the number of days in the specified month and year. This method always interprets month and year as the month and year of the Gregorian calendar even if the Gregorian calendar is not the current culture's current calendar. Syntax: public static int DaysInMonth (int year, int mont
2 min read
Decimal.Compare() Method in C#
This method is used to compare two specified Decimal values. Syntax: public static int Compare (decimal a1, decimal a2); Parameters: a1:This parameter specifies the first value to compare. a2:This parameter specifies the second value to compare. Return Value: It returns a signed number indicating th
2 min read
DateTime.Equals() Method in C#
This method is used to get a value indicating whether two DateTime objects, or a DateTime instance and another object or DateTime, have the same value. There are total 3 methods in the overload list of this method: Equals(DateTime, DateTime)Equals(DateTime)Equals(Object)Equals(DateTime, DateTime) Th
5 min read
Decimal.ToSByte() Method in C#
This method is used to convert the value of the specified Decimal to the equivalent 8-bit signed integer. A user can also convert a Decimal value to an 8-bit integer by using the Explicit assignment operator. Syntax: public static sbyte ToSByte (decimal value); Here, the value is the decimal number
2 min read