Method Overriding in C#

Let's look at an example of Method Overriding.

In my demo, I have a class named “Student” with two fields, in other words FirstName and LastName, and it also has a Print method that prints the FirstName and LastName on the console.

  1. class Student {  
  2.    public string FirstName;  
  3.    public string LastName;  
  4.    public virtual void Print() {  
  5.       Console.WriteLine(FirstName + " " + LastName);  
  6.    }  
  7. }  
This Student class acts as a base/parent class for the DiplomaStudent and GraduateStudent class.
  1. class DiplomaStudent : Student {  
  2. }  
  3. class GraduateStudent : Student {  
  4. }  
NOTE: From inheritance we know that if a derived class inherits from a base class then all the members except the private members are available in the derived class. So, this means that both of the fields and the print method can be called/invoked in the derived class.

Now, if we want to print the student details on the console screen, what we can do is we can create an instance of both of the derived student classes in our main method.
  1. class Program {  
  2.    static void Main(string[] args) {  
  3.       DiplomaStudent ds = new DiplomaStudent();  
  4.       ds.FirstName = "Max";  
  5.       ds.LastName = "Payne";  
  6.       ds.Print();  
  7.   
  8.       GraduateStudent gs = new GraduateStudent();  
  9.       gs.FirstName = "Lara";  
  10.       gs.LastName = "Croft";  
  11.       gs.Print();  
  12.    }  
  13. }  
Run the program.



Now, currently we don't know which student belongs to which type of student category. Let's say we want to add (-studentType) and append it after the student name to make it more readable.

To do that we can create the same Print method in both of derived classes.
  1. class DiplomaStudent : Student {  
  2.    public void Print() {  
  3.       Console.WriteLine(FirstName + " " + LastName + " - diploma student");  
  4.    }  
  5. }  
  6. class GraduateStudent : Student {  
  7.    public void Print() {  
  8.       Console.WriteLine(FirstName + " " + LastName + " - graduate student");  
  9.    }  
  10. }  
But we don't want to hide the base method implementation, we want to override it and to override the base method implementation, we can use the virtual keyword.

To learn more about method hiding, click the link: http://www.c-sharpcorner.com/UploadFile/219d4d/method-hiding-in-C-Sharp/
  1. class Student {  
  2.    public string FirstName;  
  3.    public string LastName;  
  4.    public virtual void Print() {  
  5.       Console.WriteLine(FirstName + " " + LastName);  
  6.    }  
  7. }  
Now, the base print method is virtual. That means we can override it in our derived class and can add the implementation as required.

But first remove the Print method from both of the derived classes as in the following:
  1. class DiplomaStudent : Student {  
  2. }  
  3. class GraduateStudent : Student {  
  4. }  
And type override then provide a space, you will see the print method in the Intellisense.



Select and press Enter. It will inherit the signature method.
  1. class DiplomaStudent : Student {  
  2.    public override void Print() {  
  3.       base.Print();  
  4.    }  
  5. }  
  6. class GraduateStudent : Student {  
  7.    public override void Print() {  
  8.       base.Print();  
  9.    }  
  10. }  
Remove it and add your own implementation as in the following:
  1. class DiplomaStudent : Student {  
  2.    public override void Print() {  
  3.       Console.WriteLine(FirstName + " " + LastName + " - diploma student");  
  4.    }  
  5. }  
  6. class GraduateStudent : Student {  
  7.    public override void Print() {  
  8.       Console.WriteLine(FirstName + " " + LastName + " -graduate student");  
  9.    }  
  10. }  
Run the program.



Now let's say for some reason I want to invoke and print the base implementation back.

We can do it by using the base keyword then a period(.) then method.
  1. class DiplomaStudent : Student {  
  2.    public override void Print() {  
  3.       base.Print();  
  4.    }  
  5. }  


In the next article, we will see the differences between Method Hiding and Method Overriding.


Similar Articles