Вы находитесь на странице: 1из 7

Introduction

Method overriding in C# is a feature like the virtual function in C++. Method overriding
is a feature that allows you to invoke functions (that have the same signatures) that
belong to different classes in the same hierarchy of inheritance using the base class
reference. C# makes use of two keywords: virtual and overrides to accomplish Method
overriding. et!s understand this through small e"amples.
P1.cs
class BC
{
public void Display()
{
System.Console.WriteLine("BC::Display");

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

class Demo
{
public static void "ain()
{
BC b;
b # ne! BC();
b.Display();

Output
BC::Display
#he above program compiles and runs successfully to give the desired output. $t consists
of a base class BC and a derived class DC. Class BC consists of function Display(). Class
DC hides the function Display() it inherited from the base class BC by providing its on
implementatin of Display(). Class Demo consists of entrypoint function "ain(). $nside
"ain() we first create a reference b of type BC. #hen we create an ob%ect of type BC and
assign its reference to reference variable b. &sing the reference variable b we invoke the
function Display(). 's e"pected( Display() of class BC is e"ecuted because the
reference variable b refers to the ob%ect of class BC.
)ow we add a twist of lime to the above program.
P2.cs
class BC
{
public void Display()
{
System.Console.WriteLine("BC::Display");

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

class Demo
{
public static void "ain()
{
BC b;
b # ne! BC();
b.Display();
b # ne! DC();
b.Display();

Output
BC::Display
BC::Display
*ere we are creating an ob%ect of +erived class DC and storing its reference in the
reference variable b of type BC. #his is valid in C#. )e"t( using the reference variable b
we invoke the function Display(). ,ince b contains a reference to ob%ect of type DC one
would e"pect the function Display() of class DC to get e"ecuted. -ut that does not
happen. $nstead what is e"ecuted is the Display() of BC class. #hat!s because the
function is invoked based on type of the reference and not to what the reference variable
b refers to. ,ince b is a reference of type BC( the function Display() of class BC will be
invoked( no matter whom b refers to. #ake one more e"ample.
P3.cs
class BC
{
public void Display()
{
System.Console.WriteLine("BC::Display");

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

class $C : BC
{
ne! public void Display()
{
System.Console.WriteLine("DC::Display");

class Demo
{
public static void "ain()
{
BC b;
b # ne! BC();
b.Display();
b # ne! DC();
b.Display();
b # ne! $C();
b.Display();

Output
BC::Display
BC::Display
BC::Display
#he output of the above program is a receipt of the fact that no matter to whom base class
reference b refers( it invokes the functions of the class that matches its type. -ut doesn!t
this sound absurd. $f b contains the reference to a perticular derived class ob%ect( then its
supposed to invoke the function of that class. /ell( C# helps us do this by the usage of
keywords virtual and override as shown in the following program.
P4.cs
class BC
{
public virtual void Display()
{
System.Console.WriteLine("BC::Display");

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

class Demo
{
public static void "ain()
{
BC b;
b # ne! BC();
b.Display();
b # ne! DC();
b.Display();

Output
BC::Display
DC::Display
#he above program compiles and runs successfully to give the e"pected desired output.
#he function Display() of -ase class BC is declared as virtual( while the +erived class!s
implementation of Display() is decorated with the modifier override. +oing so enables
C# to invoke functions like Display() based on ob%ects the reference variable refers to
and not the type of reference that is invoking the function. *ence in the above program
when b refers to the ob%ect of class BC it invokes Display() of BC and then when b refers
to the ob%ect of class DC it invokes Display() of class DC. et!s see if this holds true for
the third generation of derived classes. #ake the following program.
P4.cs
class BC
{
public virtual void Display()
{
System.Console.WriteLine("BC::Display");

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

class $C : DC
{
public override void Display()
{
System.Console.WriteLine("$C::Display");

class Demo
{
public static void "ain()
{
BC b;
b # ne! BC();
b.Display();
b # ne! DC();
b.Display();
b # ne! $C();
b.Display();

Output
BC::Display
DC::Display
$C::Display
#he above program compiles and runs successfully to give the e"pected desired output.
#he function Display() of -ase class BC is declared as virtual( while the implementation
of Display() in successive +erived classes is decorated with the modifier override.
)e"t( we succesively create ob%ects of each class and store their reference in base class
reference variable b and invoke Display(). #he rite versions of Display get invoked
based on the ob%ect the reference variable refers to. #ime for a tiny teaser0 1uess what the
output would be in the following program.
P5.cs
class BC
{
public virtual void Display()
{
System.Console.WriteLine("BC::Display");

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

class $C : DC
{

class Demo
{
public static void "ain()
{
BC b;
b # ne! $C();
b.Display();

Output
DC::Display
,ince $C has no implementation of Display()( it inherits Display() from DC as $C is
derived from DC. *ence Display() from +erived class DC gets e"ecuted. $t!s as if the
derived class $C looked like this:
class $C
{
public override void Display()
{
System.Console.WriteLine("DC::Display");

to the compiler. #ake one more e"ample. 1uess what its output will be.
P6.cs
class BC
{
public virtual void Display()
{
System.Console.WriteLine("BC::Display");

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

class $C : DC
{
public ne! void Display()
{
System.Console.WriteLine("$C::Display");

class Demo
{
public static void "ain()
{
BC b;
b # ne! $C();
b.Display();

Output
DC::Display

Вам также может понравиться