Click here to Skip to main content
15,886,110 members
Articles / Programming Languages / C#
Article

Method Hiding in C#

Rate me:
Please Sign up or sign in to vote.
3.18/5 (44 votes)
9 May 20073 min read 174.8K   43   17
A tutorial on method hiding in C#

Introduction

Method hiding in C# is similar to the function overriding feature in C++. Functions of the base class are available to the derived class. If the derived class is not happy, one of the functions available to it from the base class can define its own version of the same function with the same function signature, just differing in implementation. This new definition hides the base class definition. Take the following program for example.

P1.cs

C#
class BC
{
  public void Display()
  {
     System.Console.WriteLine("BC::Display");
  }
}

class DC : BC
{

}

class Demo
{
  public static void Main()
  {
     DC obj = new DC();
     obj.Display();
  }
}

Output

BC::Display

The above program compiles and runs successfully to give the desired output. The above program consists of a base class BC which consists of a public function named Display(). Class DC is derived from BC. So Display() of BC is Inherited by Class DC. It's as if DC has a function called Display(). Class Demo has the entry point function Main(). Inside Main we create an object obj of class DC and invoke the function Display(). What is called is the Display of BC because its inherited by the derived class DC.

For some unknown reason the derived class was not happy with the implementation of the Display() function that it had inherited from its Base class BC. So the derived class made a bold decision to define its own version of the function Display(). Thus, you could see code for Display() function in class BC as well as in class DC.

P2.cs

C#
class BC
{
  public void Display()
  {
     System.Console.WriteLine("BC::Display");
  }
}

class DC : BC
{
  public void Display()
  {
     System.Console.WriteLine("DC::Display");
  }
}

class Demo
{
  public static void Main()
  {
     DC obj = new DC();
     obj.Display();
  }
}

Output

DC::Display

On compilation the above program threw a warning which said

P2.cs(11,15): warning CS0108: 'DC.Display()' hides inherited member 
'BC.Display()'. Use the new keyword if hiding was intended. P2.cs(3,15): 
(Location of symbol related to previous warning)

The compiler warns us about Display() of DC forcibly hiding Display() of BC. The compiler smells something dicy and requests us to use the new keyword if the hiding was intentional.

The above program compiles to produce an executable. We run the executable to get the desired output as shown above. The Display() of derived class DC is invoked as it hides the Display() of base class BC. But an executable with warnings is like a neatly dressed man with unpolished shoes. We need to remove the warning. We need to tell the compiler that the implementation of Display() in derived class was intentional. We do this by using the modifier new as shown in the program below.

P3.cs

C#
class BC
{
  public void Display()
  {
     System.Console.WriteLine("BC::Display");
  }
}

class DC : BC
{
  new public void Display()
  {
     System.Console.WriteLine("DC::Display");
  }
}

class Demo
{
  public static void Main()
  {
     DC obj = new DC();
     obj.Display();
  }
}

Output

DC::Display

The addition of the modifier new to the function Display() in Derived class informs the compiler that the implementation of Display() in Derived class was intentional, thus stopping the compiler from throwing any warnings at us. Can you tell what the output of the above program will be?

P4.cs

C#
class BC
{
  public void Display()
  {
     System.Console.WriteLine("BC::Display");
  }
}

class DC : BC
{
  new public void Display()
  {
     System.Console.WriteLine("DC::Display");
  }
}

class TC : DC
{

}

class Demo
{
  public static void Main()
  {
     TC obj = new TC();
     obj.Display();
  }
}

Output

DC::Display

The above program compiles and runs successfully to give the desired output. Class TC is derived from DC. So Display() of DC is Inherited by Class TC. It's as if TC has a function called Display(). Class Demo has the entry point function Main(). Inside Main we create an object obj of class TC and invoke the function Display(). What gets called is the Display of DC because its inheritted by the derived class TC. Now what if we were to define and invoke class TC's own version of Display() ? Simple! We would have to define a function Display inside class TC with a new modifier as follows.

P5.cs

C#
class BC
{
  public void Display()
  {
     System.Console.WriteLine("BC::Display");
  }
}

class DC : BC
{
  new public void Display()
  {
     System.Console.WriteLine("DC::Display");
  }
}

class TC : DC
{
  new public void Display()
  {
     System.Console.WriteLine("TC::Display");
  }
}

class Demo
{
  public static void Main()
  {
     TC obj = new TC();
     obj.Display();
  }
}

Output

TC::Display

The above program comples and runs successfully to give the desired output.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
India India
I am a Software engineer with around 7+ years of experience. Most of my experience is in Storage technology.

Comments and Discussions

 
QuestionWhy we use Method Hiding. Pin
Member 1220373612-Dec-15 22:26
Member 1220373612-Dec-15 22:26 
QuestionAdvantages/Disadvantages Pin
yetibrain19-Mar-14 0:29
yetibrain19-Mar-14 0:29 
GeneralMy vote of 3 Pin
yetibrain19-Mar-14 0:22
yetibrain19-Mar-14 0:22 
QuestionHow do i call the base class method from child object Pin
djmak19-Sep-13 5:51
djmak19-Sep-13 5:51 
AnswerRe: How do i call the base class method from child object Pin
Rishabh1224-Feb-15 5:23
Rishabh1224-Feb-15 5:23 
GeneralMy vote of 1 Pin
Akhil Mittal16-Aug-13 1:25
professionalAkhil Mittal16-Aug-13 1:25 
GeneralRe: My vote of 1 Pin
Chetan Kudalkar24-Feb-15 19:37
Chetan Kudalkar24-Feb-15 19:37 
QuestionFound Another link ?? Pin
vivek_viv30-Aug-11 1:45
vivek_viv30-Aug-11 1:45 
AnswerRe: Found Another link ?? Pin
Chetan Kudalkar8-Sep-11 18:38
Chetan Kudalkar8-Sep-11 18:38 
GeneralMy vote of 4 Pin
Valentino_Lokesh19-Oct-10 6:53
Valentino_Lokesh19-Oct-10 6:53 
GeneralYippee! Pin
vmp_pdx20-Jan-10 14:56
vmp_pdx20-Jan-10 14:56 
General5 Stars! Pin
jp2code2-Sep-09 9:50
professionaljp2code2-Sep-09 9:50 
GeneralGood Article Pin
mdubey8221-Oct-08 22:12
mdubey8221-Oct-08 22:12 
GeneralThanks, and what about partial classes and code gen Pin
philmee959-Jun-08 14:27
philmee959-Jun-08 14:27 
Generalgood expaination Pin
nik4u13-Sep-07 18:48
nik4u13-Sep-07 18:48 
GeneralAnother one to add Pin
Rei Miyasaka9-May-07 14:10
Rei Miyasaka9-May-07 14:10 
AnswerRe: Another one to add Pin
bearfx9-May-07 14:38
bearfx9-May-07 14:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.