Home > Backend Development > C#.Net Tutorial > Method Overriding in C#

Method Overriding in C#

PHPz
Release: 2024-09-03 15:13:36
Original
1130 people have browsed it

Method Overriding is a commonly used functional operation in the C# programming, where there is a requirement for overriding the operations defined in the base class by making use of the derived class. For a successful overriding, the method is expected to be a static method, member classification and access modifiers should be of same type in both base and derived methods, and the overriding can be applied only on the derived class & not on the base class. The other names given for method overriding are run time polymorphism, dynamic polymorphism and late binding.

Method Overriding in C#

How to Use Overriding in C#?

Suppose, our application has the requirement to change the behavior of the base class method in a derived class, then we should go for method overriding. To achieve this, we need to create the method in the derived class with the same signature in the base class to perform a different task.  Before we learn how to use the overriding technique, we need to keep in mind the below points.

  • In C# programming, the Method Overriding is also called Run time polymorphism, Late binding, or Dynamic Polymorphism.
  • Only in the derived classes method overriding is possible. Because a method is overridden in the derived class from the base class.
  • The method should be a non-virtual or static method for an override.
  • Access level modifier of both the override method and the virtual method should be the same.

The method which is overridden by the override declaration is called the overridden base method. This will be present in the base class. The overridden base method can be either abstract, override or virtual. Fresh implementation called, override method is inherited from this base class.

We use different keywords for method overriding. They are,

1. Virtual Keyword

We the compiler encounters the virtual keyword in the program, it understands that this method can be overridden by any of its derived classes. The structure is as below.

public virtual int EnemyHealth()
{
Console.WriteLine("Enemy Health");
}
Copy after login

2. Override Keyword

This is present in the derived class. It tells the compiler that this method is overriding the method with the same name and signature in the base class.

public override int EnemyHealth ()
{
Console.WriteLine("Enemy 1");
}
Copy after login

Example

First, let us look into the example without using the Virtual and override keyword.

Code:

class Birds
{
public void Color( )
{
Console.WriteLine("Birds will have different Colors");
}
}
class Parrot:Birds
{
public void Color( )
{
Console.WriteLine("Parrot is Green");
}
}
class ExecutionClass
{
public static void Main( )
{
Birds object;
object = new Birds( );
object .Color( );
object = new Parrot( );
object.Color( );
}
}
Copy after login

Output:

Method Overriding in C#

In the above example, we created an object of Derived class Parrotand storing its reference in the reference variable object of the type Birds.

In the next step, using the reference variable object we invoke the function Color (). Since object contains a reference to an object of type Parrot, it is natural that we expect the function Color () of class Parrot to get executed. But we are wrong. The beauty of the programming won’t allow it to happen.

To our surprise, what is executed is the Color () method of Birds class. That’s because the function is invoked based on the type of reference and not to what the reference variable object refers to. Since the object is a reference of type Birds, the function Color () of class Birds will be invoked, no matter whom object refers to.

Now let’s rewrite the same program with virtual and override This time we will go step by step for a better understanding.

Create a base class with any name. Here I am using Birds. Write a method with the virtual keyword. This will be our overridden method which we will override in the inherited class. Inside the method write some message to print in the console.

class Birds
{
public virtual void Color( )
{
Console.WriteLine("Birds will have different Colors");
}
}
Copy after login

Now create one more class Parrot This time we will inherit it from the base class which we created earlier i.e. Birds. To inherit we use the ‘:’ symbol.

class Parrot: Birds

Here write a function with override keyword and write some message. Make sure that the method name and signature in the derived class match with the method name and signature in the base class.

public override void Color ( )
{
Console.WriteLine("Parrot is Green");
}
Copy after login

We need one more class to execute the functionality to check the overriding. Create a class with any name. Inside that write the Main function.

class ExecutionClass
{
public static void Main( )
{
}
}
Copy after login

Create an object of the parent class and trigger the function using the object of the class. This will invoke the Color method present in the Birds class.

Birds object;
object = new Birds( );
object .Color( );
Copy after login

Now create the object of the derived class and call the Color method. This will invoke the Color method of the Parrot class.

object = new Parrot( );
object.Color( );
Copy after login

Can you guess the output when we run the program? This is as shown below.

Output:

Method Overriding in C#

The Keywords override and virtual allows to invoke the base class and derived class methods separately at any point of time, even if the method names and signatures are the same.

Conclusion

In this article we understood the method overriding, need of the method overriding, real-world examples of the overriding principle, the way to achieve it, the necessity of using the virtual and override keywords and the example of overriding using the code. We can conclude that overriding is one of the useful features in polymorphism which we can change the behavior of the base class method in the derived class.

The above is the detailed content of Method Overriding in C#. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template