Click here to Skip to main content
Licence 
First Posted 9 May 2007
Views 46,467
Bookmarked 31 times

Method Hiding in C#

By | 9 May 2007 | Article
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

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

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

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

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

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

About the Author

Chetan Kudalkar

Software Developer (Senior)

India India

Member

I am a Software engineer with around 7+ years of experience. Most of my experience is in Storage technology.


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionFound Another link ?? Pinmembervivek_viv1:45 30 Aug '11  
AnswerRe: Found Another link ?? PinmemberChetan Kudalkar18:38 8 Sep '11  
GeneralMy vote of 4 Pinmemberlokeshkumarn6:53 19 Oct '10  
GeneralYippee! Pinmembervmp_pdx14:56 20 Jan '10  
General5 Stars! Pinmemberjp2code9:50 2 Sep '09  
GeneralGood Article Pinmembermdubey8222:12 21 Oct '08  
GeneralThanks, and what about partial classes and code gen Pinmemberphilmee9514:27 9 Jun '08  
Generalgood expaination Pinmembernik4u18:48 13 Sep '07  
GeneralAnother one to add Pinmemberreinux14:10 9 May '07  
AnswerRe: Another one to add Pinmemberbearfx14:38 9 May '07  

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

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 9 May 2007
Article Copyright 2007 by Chetan Kudalkar
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid