c-sharp
  1. c-sharp-method-overriding

C# Method Overriding

Method overriding is a feature in C# that allows a derived class to provide its own implementation of a method defined in the base class. In this tutorial, we'll discuss how to use method overriding in C#.

Syntax

The syntax for method overriding in C# is as follows:

class MyBaseClass {
   public virtual void MyMethod() {
      // base implementation
   }
}

class MyDerivedClass : MyBaseClass {
   public override void MyMethod() {
      // derived class implementation
   }
}

The derived class must use the "override" keyword to indicate that it is overriding a method in the base class. The base class must use the "virtual" keyword to indicate that it is allowing derived classes to override the method.

Example

Let's say we have a base class called "Animal" and a derived class called "Dog" that inherits from Animal. We want to define a method called "MakeSound" that each animal can implement differently. Here's how we can implement it:

class Animal {
   public virtual void MakeSound() {
      Console.WriteLine("The animal makes a sound.");
   }
}

class Dog : Animal {
   public override void MakeSound() {
      Console.WriteLine("The dog barks.");
   }
}

Now, we can create instances of the classes and call the "MakeSound" method:

Animal animal = new Animal();
Dog dog = new Dog();

animal.MakeSound(); // Output: The animal makes a sound.
dog.MakeSound(); // Output: The dog barks.

Output

When we run the example code above, the output will be:

The animal makes a sound.
The dog barks.

This is because we created an instance of the Animal class and called the "MakeSound" method, which printed "The animal makes a sound" to the console. We then created an instance of the Dog class and called the "MakeSound" method, which printed "The dog barks" to the console.

Explanation

In the example above, we defined a base class called "Animal" that has a virtual method called "MakeSound". We then defined a derived class called "Dog" that overrides the "MakeSound" method with its own implementation.

When we create an instance of the Animal class and call the "MakeSound" method, it calls the base implementation, which prints "The animal makes a sound." to the console. When we create an instance of the Dog class and call the "MakeSound" method, it calls the derived implementation, which prints "The dog barks." to the console.

Use

Method overriding is useful when you have a base class that defines a default implementation of a method, but you want derived classes to be able to customize that method for their own specific needs.

Important Points

  • The signature (name, return type, and parameters) of the overriding method must be the same as the base method.
  • The access modifier of the overriding method can't be more restrictive than the access modifier of the base method.
  • When overriding a method, use the "base" keyword to call the base implementation of the method.

Summary

In this tutorial, we discussed how to use method overriding in C#. We covered the syntax, example, output, explanation, use, and important points of method overriding in C#. With this knowledge, you can now use method overriding in your C# code to provide custom implementations of methods in derived classes.

Published on: