C# Inheritance

Object-oriented programming

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.

Object

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

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 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.

List
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
This section provides information

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

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.

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

AsIsVirtual 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.

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

Interface Types
Error 1:

Class Python cannot have multiple base classes: Net and Perl.

Summary

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, are superior.


C#: Class