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
ofBC
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 functionDisplay()
. 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
. SoDisplay()
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 classTC
'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.
No comments:
Post a Comment