Inheritance In C#

What is Inheritance in C#?

Inheritance is a powerful feature of Object Oriented Programming languages. Using inheritance, you create base classes that encapsulate common functionality. The base class is inherited by derived classes. The derived classes inherit the properties and methods of the base class. The .Net Framework uses inheritance throughout. For example, the CollectionBase class contains properties and methods required by most collections. It contains properties such as Capacity and Count and methods such as Clear and Remove At.

In order to use the base class in another class (referred to as the derived class) you reference the base class by placing a colon after the name of the derived class and then place the name of the base class. The following code defines a base class RentalItem and two derived classes, DVD and Book, that inherit the base class.

public class RentalItem
{
    private string _title;
    public string Title
    {
        get { return _title; }
        set { _title = value; }
    }
    public bool CheckStock(string title)
    {
        // Code to check if in stock.
        // Returns true for demo.
        return true;
    }
}
public class DVD : RentalItem
{
}
public class Book : RentalItem
{

}

The derived classes inherit the base class's properties and methods, and clients of the derived class have no knowledge of the base class. The derived class is not limited to the properties and methods of the base class. They can further define properties and methods as needed. The following code shows the derived class Book defining its own property ISBN.

public class Book : RentalItem
{
    private string _ISBN;
    public string ISBN
    {
        get { return _ISBN; }
        set { _ISBN = value; }
    }
}

Clients of the Book class will be exposed to both property Title and ISBN, as shown in the following screenshot.

OOPC1.gif

There are many times when the derived class needs to use the functionality of the base class but also needs to add some functionality unique to the derived class. In this case, the derived DVD class uses the keyword base to call the base class RentalItem's CheckStock method, as shown in the following code.

public class DVD : RentalItem
{
    public bool Rent(string title, int age)
    {
        if (base.CheckStock(title))
        {
            // Code to check rating against the age of the renter
            // Returns true for demo
            return true;
        }
        else
        {
            return false;
        }
    }
}

Methods inherited by the derived class can be overloaded just as you would overload any method in the same class. The method names are the same, but the method signatures are different. The following code shows the Book class overloading the CheckStock method of the RentalItem base class.

public class Book : RentalItem
{
    private string _ISBN;
    public string ISBN
    {
        get { return _ISBN; }
        set { _ISBN = value; }
    }
    public bool CheckStock(string title, string author)
    {
        // Code to check if in stock.
        // Returns true for demo.
        return true;
    }
}

A client of the Book class will see both methods, as shown in the following screenshot.

OOPC2.gif

A method of the derived class can override a method of the base class using the override keyword. For example, the Book class can override the CheckStock method of the RentalItem base class to pass in the ISBN instead of the title.

public override bool CheckStock(string ISBN)
{
    // Code to check if in stock.
    // Returns true for demo.
    return true;
}

In this case, clients of the Book class could use the overridden method and would not see the RentalItem's method.

OOPC3.gif

At this point, a client can instantiate an instance of both the Book class and the RentalItem class.

There are times when the base class is not designed to be instantiated directly by clients; rather, it is designed to be inherited by derived classes that are then exposed to clients. These types of base classes are called abstract classes. To create abstract classes, use the abstract keyword when defining the base class.

public abstract class RentalItem
{
    private string _title;
    public string Title
    {
        get { return _title; }
        set { _title = value; }
    }
    public bool CheckStock(string title)
    {
        // Code to check if in stock.
        // Returns true for demo.
        return true;
    }
}

The .Net Framework contains many abstract classes. For example, the TextReader class is the abstract base class of the StreamReader and StringReader classes.

In this article, you looked at inheritance and how it is used in C# programming. It is important that you understand this concept to efficiently program in C# and effectively work with the .NET framework. For more information on object oriented programming, C#, and working with the .NET framework, refer to my book "Beginning C# Object Oriented Programming" from Apress publishers.


Similar Articles