C# Inheritance Explanation

Inheritance makes programs simpler and faster. With inheritance, you build several types upon a common abstraction, and then can later act upon all those types through the abstraction. We examine inheritance and polymorphism in the C# language.

Object-oriented programming

Base class: The class another class inherits from.

Derived class: The class with a base class.

Example

Note

When you specify that a class derives from another class, the class you create acquires all the features of the base class. If the derived class has a few additional features but is otherwise the same as the base class, this is a good approach to use. Also, by using a base class and many derived classes, you can treat all those derived classes as the same base type and call virtual functions to access their features.

List Examples
Program using base class, virtual method [C#]

using System;
using System.Collections.Generic;

class Net
{
    public virtual void Act()
    {
    }
}

class Perl : Net
{
    public override void Act()
    {
	Console.WriteLine("Perl.Act");
    }
}

class Python : Net
{
    public override void Act()
    {
	Console.WriteLine("Python.Act");
    }
}

class Program
{
    static void Main()
    {
	// Use base class and derived types in a List.
	List<Net> nets = new List<Net>();
	nets.Add(new Perl());
	nets.Add(new Python());

	// Call virtual method on each instance.
	foreach (Net net in nets)
	{
	    net.Act();
	}
    }
}

Output

Perl.Act
Python.Act

Description. This program shows how you can use a simple object model that features two derived classes from one base class. We create a List of the base class type, and then add derived classes to it. When we finally call the Act method through the base class references in the List, we invoke the overridden functions through the virtual base function. Thus the derived types retain all their features but share a type.

Casting

Virtual keyword

It is possible to use cast operators to test or convert a base type to a more derived type. However, in many cases, if your program uses a base class but has to cast to more derived types, it may have a design flaw. Instead of special-casing based on the derived type, it is often better to introduce a new virtual method on the base class and implement it in the derived type.

As Cast Example Is Cast Example Virtual Method

Multiple base classes

Programming tip

You cannot specify multiple base classes on a type declaration. This restriction was imposed on the C# language to reduce the complexity of inheritance and eliminate some problems. You can, however, implement multiple interfaces. You can also implement interfaces and also inherit from a single base class.

Interface Types

Error 1 Class 'Python' cannot have multiple base classes: 'Net' and 'Perl'

Summary

The C# programming language

We looked at some of the practical and design issues related to inheritance in the C# language. Inheritance enables you to create complex models that can reduce your program size and improve its modularity and performance. It is important, though, not to overuse or abuse inheritance. Often, simpler solutions where a common abstraction is not specified may be superior.

Class Examples
.NET