Example of C# Method Overriding
class base_class
{
public void ninja();
}
class derived_class : base_class
{
public void ninja();
}
class Main_Method
{
static void Main()
{
derived_class d = new derived_class();
d.ninja();
}
}
Keywords in Method Overriding
There are three types of keywords for overriding:
virtual keyword
This modifier or keyword is used within the base class method. It modifies a method in the base class to override that particular method in the derived class, i.e., it tells the compiler that derived classes can override this method. For example, the virtual keyword can be used in a code in the following format:
public virtual int myValue()
{
Console.WriteLine("Base-Value 2");
}
override keyword
This modifier or keyword is used with the derived class method. It modifies a virtual or abstract method into the derived class presented in the base class. You can use this keyword in a code as in the following example:
class B : A
{
public override void show()
{
Console.WriteLine("Hello: Derived Class!");
Console.ReadLine();
}
}
base keyword
This is used to access members of the base class from a derived class. It is used to access the base class's constructors and methods or functions. You cannot use the base keyword within a static method. The base keyword specifies which base class's constructor should be invoked while creating the derived-class instances.
You can use the base keyword in call methods or functions of a base class from a derived class and call the base class's constructor at the time of inheritance. You can use the base keyword in a code as in the following example:
class Employee : Person
{ public string id = "ABC567EFG";
public override void GetInfo()
{
// Calling the base class GetInfo with the help of base keyword
base.GetInfo();
Console.WriteLine("Employee ID: {0}", id);
}
}
Prevention of overriding in C#
In C#, method overriding allows a subclass to provide its own implementation of a method defined in a base class. However, there are situations where you might want to prevent a method from being overridden in derived classes. C# provides mechanisms to enforce this.
To prevent method overriding, you can use the sealed keyword or declare the method as static.
1. Using the sealed Keyword
The sealed keyword is used to prevent further overriding of a method in any class that inherits from a class where the method has been overridden. When a method is marked as sealed, no subclass can override it anymore.
Syntax:
class BaseClass
{
public virtual void Display()
{
Console.WriteLine("Base class method");
}
}
class DerivedClass : BaseClass
{
public override void Display()
{
Console.WriteLine("Derived class method");
}
}
class FurtherDerivedClass : DerivedClass
{
public override void Display() // Error: Cannot override a sealed method
{
Console.WriteLine("Further derived class method");
}
}
Explanation: In the example above, the Display method in DerivedClass is overridden and marked as sealed. Therefore, any further attempt to override it in a subclass, like FurtherDerivedClass, will result in a compile-time error.
2. Declaring the Method as static
Another way to prevent overriding is by declaring the method as static. Static methods belong to the class itself, not to instances of the class, so they cannot be overridden in derived classes.
Syntax:
class BaseClass
{
public static void Display()
{
Console.WriteLine("Base class static method");
}
}
class DerivedClass : BaseClass
{
public static new void Display() // Hides the base class static method
{
Console.WriteLine("Derived class static method");
}
}
Explanation: The Display method in the base class is static, and while you can hide it in the derived class using the new keyword, you cannot override it. Static methods are resolved at compile time, not runtime, so they don't participate in polymorphism or method overriding.
Frequently Asked Questions
What is method overriding?
When a method defined in the parent class has a specific implementation in the child class or the creation of a method in the child class with the same definition as the method in the parent class is called Method Overriding.
Why is overriding used?
Overriding is used to provide a specific implementation of a method in a subclass, allowing polymorphism and enabling behavior customization for different object types.
Why we use method overriding in C#?
If derived class defines same method as defined in its base class, it is known as method overriding in C#. It is used to achieve runtime polymorphism. It enables you to provide specific implementation of the method which is already provided by its base class.
Conclusion
In this article, we have extensively discussed we will learn about the C# language's method overriding concepts. Having gone through this article, I am sure you must be excited to read similar blogs. Coding Ninjas has got you covered. Here are some similar blogs to redirect: C# Method Parameters, Methods in C#, Introduction to C#, C# Type Conversion and Properties of C# We hope that this blog has helped you enhance your knowledge, and if you wish to learn more, check out our Code360 Blog site and visit our Library. Here are some courses provided by Code360: Basics of C++ with DSA, Competitive Programming and MERN Stack Web Development. Do upvote our blog to help other ninjas grow.