Understanding the C# Override Operator

c# override operatorThe foundation of any object-oriented language is laid on three pillars: Inheritance, Encapsulation and Polymorphism. C#, like all other object oriented languages support these features. Of the three core features of an object-oriented languages, inheritance is the most useful and most fascinating feature since it fosters code organization and reusability. Inheritance in OOP is similar to the inheritance in real life. People inherit characteristics of their parents; for instance tall people usually have tall parents and short people have shorter. Similarly, children can have some characteristics that were not necessarily present in their parents.

Inheritance in programming is based on the same idea where child classes inherit characteristic of the parent classes. In addition they contain some intrinsic characteristics that are not present in the parent class. The C# override operator is an operator that overrides the variables or methods in the parent class with the same name and provides its own implementations for those properties or methods.

To understand the role of the C# override operator in OOP, it is necessary to have a solid understanding of inheritance in C#. Therefore, before diving into its usage, it is a good idea to review how inheritance is implemented in C#.

Interested in learning more about C#? Take this course at Udemy.com

Inheritance: The backbone of Object Oriented Programming

As we mentioned before, inheritance thrives on the idea of real life inheritance.  A typical example of inheritance is the relationship between Employee and a Manager class and an Employee and SalesMan class. A point worth noting here is that inheritance signifies the is-a relationship between a child and parent class.

For instance, a Manager is an Employee. A SalesMan is also an Employee. Both a Manager and a SalesMan inherit some common properties from their parent class, Employee. For instance, a Manager and SalesMan can have a name attribute, an id attribute, and a salary attribute. However, a Manager can have an additional attribute of vehicle_assigned. Similarly, a SalesMan can have an additional attribute of commission.

In simpler terms, a Manager is assigned a vehicle by the company and this property is intrinsic to the Manager class. A vehicle is not assigned to a SalesMan. Similarly, the commission attribute is intrinsic to a SalesMan and it denotes the percentage of the commission which a SalesMan gets on each sale. In this Scenario, it is assumed that a Manager gets no commission, therefore this attribute has not been encapsulated in the Manager class. In short, common attributes are packaged into a parent class and unique attributes are packaged into child classes.

The following C# code implements the above mentioned scenario.

class Employee

{

public string name;

public int id;

public long salary;

}
 
class Manager : Employee

{

public string assigned_vehicle;

}

class SalesMan : Employee

{

public int commission;

}

For more detailed C# courses, see this course at Udemy.com

Using C# Override Operator

In the above code snippet, the concept of inheritance is explained since it is necessary to have a solid understanding of inheritance before jumping into overriding. This section deals with the explanation of the C# Override operator.

Virtual Method

A parent class can have a concrete method that is as-it-is inherited by the derived classes. In addition, a parent class can have a virtual method. A virtual method is placed in a parent class where the implementation can be replaced in the derived classes. To declare a virtual method, a keyword virtual has to be prefixed before the return type of the method.

For instance, if a virtual method must be declared to display the information about the employee. The Employee class can be modified as follows:

class Employee

{

public string name;

public int id;

public long salary;

public virtual void EmpInfo()

{

Console.WriteLine("This class contains information about the Employee");

}

}

The above class contains a method called EmpInfo, and for the sake of explanation, a string value is  printed on the console screen.

Now, if the derived classes override this EmpInfo method and provide its own implementation when the object of that child class is created and the EmpInfo method is called from that object, the overridden EmpInfo method in that child class is called. Similarly, if the EmpInfo method is called on the object of the class that doesn’t override this method, the EmpInfo method of the parent class is called.

The following example demonstrates this concept.

In the Manager class that inherits the Employee class, override the EmpInfo method. This is shown in the following code:

class Manager : Employee

{

public string assigned_vehicle;

public override void EmpInfo()

{

Console.WriteLine("This is the child class manager");

}

}

Assume that the SalesMan class doesn’t override the EmpInfo method.

If the objects of both of these classes are created and EmpInfo method is called on them, the EmpInfo method overridden by the Manager class is executed by the object of the Manager class. On the other hand, the EmpInfo method of the parent Employee class is executed by the object of the SalesMan class since it doesn’t override it.  The following code snippet demonstrates this:

Manager mgr = new Manager();

mgr.EmpInfo();

SalesMan sm = new SalesMan();

sm.EmpInfo();

On the console output, the following lines are displayed:

This is the child class manager

This class contains information about the Employee

The first line is displayed by the overridden EmpInfo method in the Manager class while the second line is displayed by the object of SalesMan class that doesn’t override the EmpInfo method.

The C# operator overloading is just one of the hundreds of advanced features that come with object oriented languages in general and C# in particular. Operator overloading, Abstract Class, Polymorphism, Encapsulation are some of those advanced and extremely useful features. Udemy.com contains detailed courses on C# and object oriented programming for all variety of audiences ranging from beginners to professional developers.

You can find a great course to help you learn C# and advanced coding at Udemy.com