Skip to content

Inheritance

Object-oriented programming (OOP) inheritance is a fundamental concept that allows a new class (subclass or derived class) to inherit properties and behaviors (fields and methods) from an existing class (base class or superclass). Inheritance facilitates code reuse, extensibility, and the creation of a hierarchy of related classes. The derived class inherits the members of the base class and can also provide its own additional members or override existing ones.

Declaring Derived Classes

C#
public class Employee
{

}

// CommissionEmployee derives from (inherits) Employee
public class CommissionEmployee : Employee
{

}

The : symbol is used to indicate that a class derives from another class. In the example above, the CommissionEmployee class inherits from the Employee class.

Invoking Base Class Constructors

When you covered inheritance in the previous level of programming, you learned that a base class constructor must always be invoked by a derived class constructor. Depending how the base class is coded, you may be required to explicitly invoke a base class constructor.

C#
public class Employee
{
    public string Name
    {
        get; 
        private set;
    }

    public Employee(string name)
    {
        this.Name = name;
    }
}

public class CommissionEmployee : Employee
{
    public CommissionEmployee(string name) : base(name)
    {

    }
}

In the above example code, you can see the call to the base class constructor using the base keyword. This statement is coded on the same line as the declaration of the constructor, separated by the : symbol.

Note

When a base class is defined with a Default Constructor or a constructor with no parameters, the call to the base class constructor can happen implicitly.

Danger

If you are receiving the "CS7036: There is no argument given that corresponds to the required formal parameter...", it is likely you are not invoking the base class constructor when you should be doing this explicitly.

Virtual Methods and Properties

A virtual method is a method that can be overridden in a derived class. In C#, methods and properties are not virtual by default. To allow a method or property to be overridden, you must declare it using the virtual keyword.

C#
public class Employee
{
    public virtual string Name
    {
        get; 
        private set;
    }

    public virtual decimal GetPay()
    {

    }
}

Method and Property Overriding

To override a method or property, you must use the override keyword.

C#
public class CommissionEmployee : Employee
{
    public override string Name
    {
        get
        {

        }
    }

    public override decimal GetPay()
    {

    }
}

When you want to include the base class implementation in the derived class version of the method, you can invoke it using the base keyword.

C#
public class CommissionEmployee : Employee
{
    public override decimal GetPay()
    {
        return base.GetPay() * this.CommissionRate;
    }
}

Warning

Omitting the override keyword will not cause a syntax error, but will not result in method overriding.

Abstract Classes and Members

When a class is defined solely for the purposes of being a base class, you will declare the class using the abstract keyword.

C#
public abstract class Employee
{

}

Note

Abstract classes cannot be directly instantiated.

Methods and properties that cannot be implemented in the base class, are declared using the abstract keyword and do not have a code block.

C#
public abstract class Employee
{
    public abstract decimal GetPay();
}

Class members declared as abstract must be implemented in the derived concrete class.

C#
public class CommissionEmployee : Employee
{
    public override decimal GetPay()
    {

    }
}

Properties can also be declared as abstract. If the GetPay() method was coded as a property, the property would need to be overridden in the concrete class.

C#
public abstract class Employee
{
    public abstract decimal Pay
    {
        get;
    }
}

public class CommissionEmployee : Employee
{
    public override decimal Pay
    {
        get
        {

        }
    }
}

Why Use Inheritance

  1. Code Reusability: Inheritance promotes code reuse by allowing the derived class to inherit the properties and methods of the base class. This avoids duplicating common code.

  2. Extensibility: You can extend the functionality of existing classes without modifying their code. New classes can be created based on existing ones and enhanced with additional features.

  3. Polymorphism: Inheritance supports polymorphism, allowing objects of the derived class to be treated as objects of the base class. This facilitates flexibility and the use of a common interface.

  4. Organization and Abstraction: Inheritance helps in organizing classes into a hierarchy, providing a clear structure that reflects the relationships between different entities in the system.

  5. Maintenance: Changes made to the base class automatically affect all derived classes, reducing the effort required to maintain and update code.

Further Reading