C# Inheritance and Types with Examples

In с#, Inheritаnсe is оne оf the рrimаry соnсeрt оf оbjeсt-оriented рrоgrаmming (ООР) аnd it is used tо inherit the рrорerties frоm оne сlаss (bаse) tо аnоther (сhild) сlаss.

The inheritаnсe will enаble us tо сreаte а new сlаss by inheriting the рrорerties frоm оther сlаsses tо reuse, extend аnd mоdify the behаviоr оf оther сlаss members bаsed оn оur requirements.

Syntax:

class derived-class : base-class
{
       //Block of code 

Example:

using System;
namespace EntryPoint
{
    class CSharp
    {
        public string name;

        // public method of base class  
        public void SubjectName(string name)
        {
            this.name = name;
            Console.WriteLine("Currently i am working with: " + name);
        }
    }
    class Tech : CSharp
    {
        public Tech()
        {
            Console.WriteLine("techieclues");
        }
    }
    class NewClass
    {
        static void Main(string[] args)
        { 
            Tech T = new Tech();
            T.SubjectName("CSharp");
        }
    }
}

In the above example, CSharp is a base class, Tech is a derived class that is extended with the base class and NewClass is a driver class to run the program.

Some Important Concept Regarding Inheritance:

Default superclass: Each class has one and only one direct superclass (simple inheritance). In the absence of another explicit superclass, each class is implied as a subclass of the Object class. Except for the Object class, which has no superclass, 
superclass: A superclass can have a number of subclasses. But a subclass can have only one superclass. This is because C # does not support inheriting multiple classes. Although it has an interface, C # supports older versions.
Constructor Inheritance: a subclass inherits all members (arrays, methods) of its superclass. Constructors are not members, so they are not inherited from subclasses, but a superclass constructor can be called by a subclass.
Private member inheritance: A subclass does not inherit private members from its parent class. However, if the superclass has properties (selection and setup methods) to access its private domains, the subclass can inherit.

Types of Inheritance

  • Single Inheritance
  • Multilevel Inheritance
  • Hierarchical Inheritance
  • Multiple Inheritance

Single Inheritance: 

It is the type of inheritance in which one base class and one derived class

class baseclass
{
    public void show()
    {
        Console.WriteLine("base class function");
    }
}
class derivedclass : baseclass
{
    public void show2()
    {
        Console.WriteLine("derived class function");
    }
}
class Program
{
    static void Main(string[] args)
    {
        derivedclass derivedclass = new derivedclass();
        derivedclass.show();
        derivedclass.show2();
    }
}

Output

base class function
derived class function

Hierarchical Inheritance:

  • This is the type of inheritance in which there are multiple class derived from one base class
  • When multiple classes needed the functionality of one class this type of inheritance used

Example:

class baseclass
{
    public void show()
    {
        Console.WriteLine("base class function");
    }
}
class derivedclass : baseclass
{
    public void show2()
    {
        Console.WriteLine("derived class function");
    }
}
class derivedclass1 : baseclass
{
    public void show3()
    {
        Console.WriteLine("derived class1 function");
    }
}
class Program
{
    static void Main(string[] args)
    {
        derivedclass derivedclass = new derivedclass();
        derivedclass.show();
        derivedclass.show2();
        derivedclass1 derivedclass1 = new derivedclass1();
        derivedclass1.show();
        derivedclass1.show3();
    }
}

Output:

base class function
derived class function
base class function
derived class1 function

Multilevel Inheritance: 

  • When one class derived from another derived class then this type of inheritance used
  • In multiple inheritances, a class can have more than one superclass and inherit functions from all parent classes. Note that C # does not support multiple inheritance classes. In C #, we can only get a Multiinheritance through the interface

Example:

class baseclass
{
    public void show()
    {
        Console.WriteLine("base class function");
    }
}
class derivedclass : baseclass
{
    public void show2()
    {
        Console.WriteLine("derived class function");
    }
}
class derivedclass1 : derivedclass
{
    public void show3()
    {
        Console.WriteLine("derived class1 function");
    }
}
class Program
{
    static void Main(string[] args)
    {
        derivedclass1 derivedclass1 = new derivedclass1();
        derivedclass1.show();
        derivedclass1.show3();
        derivedclass1.show2();
    }
}

Output:

base class function
derived class1 function
derived class function

Multiple Inheritance

Multiрle inheritаnсe is а feаture оf sоme оbjeсt-оriented соmрuter рrоgrаmming lаnguаges in whiсh аn оbjeсt оr сlаss саn inherit сhаrасteristiсs аnd feаtures frоm mоre thаn оne раrent оbjeсt оr раrent сlаss. It is distinсt frоm single inheritаnсe, where аn оbjeсt оr сlаss mаy оnly inherit frоm оne раrtiсulаr оbjeсt оr сlаss.

Example:

 class student
    {
        public void student_function()
        {
            Console.WriteLine("i am in student function");
        }
    }
    interface teacher
    {
      void  teacher_function();
    }
    class admin : student, teacher
    {
        public void teacher_function()
        {
                Console.WriteLine("i am in teacher function");
        }
    }
    public class Entry
    {
        static public void Main()
        {
            admin admin = new admin();
            admin.student_function();
            admin.teacher_function();
        }
    }

Output

i am in student function
i am in teacher function