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

Base class: The class another class inherits from. In the C# language, the ultimate base class is the object class.

Derived class: The class with a base class. A derived class may have multiple levels of base classes.

Example. 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 extra features but is otherwise the same as the base class, this is a good approach.

Also: By using a base class, you can treat derived classes as the same base type and call virtual functions to access their features.

What does this program show? It uses 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. 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.

Note: It is often better to introduce a new virtual method on the base class and implement it in the derived type.

Multiple base classes. 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.

Also: You can combine the concepts—both implement interfaces and inherit from a single base class.