Sie sind auf Seite 1von 6

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

Hide   Copy Code

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

Hide   Copy Code

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

Hide   Copy Code

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

Hide   Copy Code

DC::Display

On compilation the above program threw a warning which said
Hide   Copy Code

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

Hide   Copy Code

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
Hide   Copy Code

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

Hide   Shrink

  Copy Code

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
Hide   Copy Code

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

Hide   Shrink

  Copy Code

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

Hide   Copy Code

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

Das könnte Ihnen auch gefallen