C#: Inheritance or specialization

This entry is part 50 of 71 in the series C#

As mentioned in an earlier post, inheritance is one of the core ideas of Object-Oriented programming.

As compared to C++ and Java, inheritance in C# is much more refined.  For example, in C# a class can only extend one other class. This is unlike C++ where multiple inheritance is allowed.

The benefits of inheritance are obvious. Inheritance gives you the opportunity to add some functionality that does not exist in the original class.

It also gives you the chance to change the behaviors of the existing class to better suit your needs.

To put it simply, inheritance enables you to create new classes that reuse, extend, and modify the behavior that is defined in other classes.

Lets imagine your colleague has developed the following code for a company.  It has one class Staff and it can only calculate the total salary of the generat employees in your company.

In another words, you need specialization.  You need to extend it and calculate also the total salary of engineers in your company.

We will look at how we can extend the code below in the next post.

using System;

namespace Company
    public class Staff
        protected double rate;

        public Staff()
            rate = 1;

        public void salary()
            double totalSalary = 30*8*rate;


    class Program
        static void Main()
            Staff staff = new Staff();


Series Navigation<< C#: The switch statement
C#: Extending the staff base class >>

Leave a comment

Leave a Reply