c sharp

Overriding Keywords in C#

In C# programming, we use the keyword “override” to change the base class virtual member with the derived class member definition. The signature of an override method must match that of the overridden base method. In the drive class, the overriding function allows calling a function of the base class. The method overriding means building a method in the child class having a similar signature as in the parent class.

For the overriding method, we have three types of keywords that we used in C# programming.

  • Virtual keyword
  • Base keyword
  • Override

Example1: Using Virtual and Override Keywords

In this given example, we are using the keywords “virtual” and “override” for overriding the C# program in Ubuntu 20.04.

In the first step, we import the C# library which is “using System” that accesses the required functions and methods of C# programming. We created a class named “Dog” which is a base class. Inside this base class, we have a method “public virtual void print()”. In this method, “virtual” is a keyword that permits the derived class method to override this virtual method. In this virtual method, we print two lines using the “Console.WriteLine()” function. The code written in these “ConsoleWriteLine” brackets will print on the screen.

After this, we have a derived class named “Cat” inherited from the base class “Dog”. We have the “public override void print()” method in this derived class. This “print()” method is the same as we declare in the above base class. It will override the virtual method of the base class because the virtual keyword gives the authority to the base class to override its method.

After this, we have the “Console.WriteLine()” function which displays the data on the screen. Now, we declare another class named “Animal”. The “static void Main(string[] args)” is the main method of this “Animal” class. This “string[ ] args ” signifies the arguments. After this main function, we develop the object of the base class with the name “D”. In this reference variable of a base class “Dog D”, we store the object of the child class “Cat()”. We write it as “Dog D = new Cat();”. After this, when we type “D.print()” the method of the derived class named “Cat” is called because we override the method of the parent class.

We will print the output of the above C# program in Ubuntu 20.04 by using the commands which are given in the image below. Before executing this C# code, we have to compile this code first. For this, we use the “mcs” command with filename and extension “.cs”. and for the execution of this C# program, we use the “mono” command with the same filename and extension “.exe”.

In this output, we see that it overrides the method of the base class and prints the data which is written inside the derived class method.

Example2: Other Way in Using Virtual and Override Keywords

Now, we are going to discuss another example of the C# program to override the method of the base class by using “virtual” and “override” keywords.

Here, we have a “using System” statement in which “System” is a namespace. Then we have a class having the name “Base”. Inside this “Base” class, we define a method “public virtual void show()”. In this method, we are using the keyword “virtual”. This “virtual” keyword allows overriding its method. After this, we have the “Console.WriteLine” function which is used to display the output. The “Console.WriteLine(“Hello!!!!”)” will print the same “Hello!!!” on the output screen and “Console.WriteLine(“This is base class”)” will display the same text written in brackets on the screen.

After this, we have another class which is a derived class having the name “Derived”. This “Derived” class is one that we inherit from the “Base” class. Inside this “Derived” class, we have “public override void show()” which is used for overriding the method of the base class. As we know, “override” is a keyword that we discussed in detail in the above example. Now, inside the curly braces, we have to print by using the “Console.WriteLine” function. For printing “Hey!!!” we write this line of code “Console.WriteLine(“Hey!!!!”)”. It will display “Hey!!!!” on the screen. After this, we have another “Console.WriteLine” function to print “This is Derived class” on the screen.

Then, outside the curly braces, we have another class named “ABC”. In this class, we have the main method. Now, the base class object is named “obj”. The “obj= new Base()” is utilized to generate an object with the name “obj”. The “new” is a keyword that is utilized to make a new object of the class. The “obj. show()” invokes the show method of the “Base” class. After this, “obj= new Derived()” creates the same obj for the derived class. Then “obj.show()” will invoke the method of the relevant “Derived” class.

We obtain the output by using the command which we explained in the above C# program but we use the filename of this C# program.

This output shows that first, we print the message of the base class which is written inside the virtual method. Then the “override” keyword overrides the “virtual” method of the “Base” class and prints the message of the relevant “Derived” class.

Example3: Using the Base Keyword

In this example, we will discuss how to use the keyword “base” in the C# program. For overriding, we use the keyword “base”. Now we will show you how it works. Now have a look at the example that is given below.

Initially, we have “using System”. Then we have a public class with the name “Course” which is the base class. Inside this base class, we have a variable named “str1” of datatype “string” and assign a string “Information Technology” to “str1”. Now, we have a “public virtual void showdata()” in which showdata() is a virtual method.

After that, we have “Console.WriteLine” for printing as we already discussed in the previous examples. Then, we declare another class named “Syllabus” which is the derived class and inherited from the “Course” class. Here, we have another string variable named “str2” and also assign string data to this string variable. After this, we have “public override void showdata()” in which we override the showdata() method of the base class. Below this, we have “base.showdata()” which is used for calling the base showdata() method with the help of the keyword “base”.

Now, again we have the “Console.WriteLine” method. After this, there is another class having the name “Degree”. Inside this “Degree” class, we have a main method. Now, the object of class “Syllabus” is “S” which also acts as the object of the “Course” class. Then, “S.showdata()” will invoke the showdata() of the “Course” class first and then invokes the same method of class “Syllabus”.

The output of the above code is shown in the image below. We see that it prints the data of base class “Course” first and then prints the data of derived class “Syllabus”.

Conclusion

In this article, we understand the notion of overriding in detail with examples. As we learned that for overriding, we must have a base class and derived class. In this, we see that the overriding method is used in the derived class only. We can’t override a static method. Also, we can’t override the non-virtual method. For achieving “run time polymorphism” C# overriding method is used. We use all three keywords “virtual”, “override”, and “base” in this article and studied them in detail with the help of examples. These illustrations assist a lot in understanding the concept of overriding in the C# programming language. This article will enhance your knowledge about the overriding concept and it will be helpful for you in the future.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.