Inheritance in C#

Inheritance is the process through which a Class derives properties from another Class. A Class that inherits the Properties of another Class is called a Child Class or Derived Class, Whereas , the Class from which the Child Class is derived is known as a Parent Class or Base Class.

A Parent Class is at higher level in the Class hierarchy. A Class hierarchy defines logical structuring of the Classes such that a general Class is on the top of the Class hierarchy and more Specialized Classes over at lower level.

Inheritance is one of the Primary pillar of Object Oriented Programming .

Following are some of the useful points regarding to Inheritance.

  • Inheritance allows the code reusability.
  • Code reuse can reduce the Time and Error. You will specify all the common fields,Properties,methods in the base Class which allows reusability. In the derived class you will only have fields, properties ,and methods that are specific to them.
  • C# Support only single Class inheritance.
  • C# Support Multilevel Inheritance.
  • C# does not support multiple inheritance through Classes. To implement multiple inheritance we need to use Interfaces.
  • C# support multiple Interface Inheritance.
  • Base Classes are automatically instantiated before the derived classes.
  • Parent Class Constructor executes before the child classs constructor.

Single Inheritance :- Single Inheritance means a derived class can have only one Base Class.

Image is not available

Multilevel Inheritance :- Multilevel Inheritance means a child class is derived from a class , which in turn is derived from another class.

Image is not available

More on Inheritance

Inheritance Implementation :- Single Inheritance

                
public class RoadTransport : Transport
    {
        public string VechiecleNumber;
        public string NameOfDriver;
        public string DriverContactNumber;
        public int CapacityOfVechiecle;
    }           
                
Image is not available
                
 public class Transport
    {
        public string DepartmentId;
        public string Head;
        public string Location;
        public string TypesOfTransport;
        public int NumberOfVechiecles;

        public int ReturnVechiecleCounts(string TypesOfTransport)
        {
            int availableVechiecleCounts=0;

            if(TypesOfTransport=="Road")
            {
                availableVechiecleCounts = 15;
            }
            if (TypesOfTransport == "Air")
            {
                availableVechiecleCounts = 2;
            }
            if (TypesOfTransport == "Train")
            {
                availableVechiecleCounts = 6;
            }
            return availableVechiecleCounts;
        }
    }           
                
                
 internal class Program
    {
        static void Main(string[] args)
        {
           RoadTransport roadTransport = new RoadTransport();
           int availableVechiecleCounts = roadTransport.ReturnVechiecleCounts("Road");
           Console.WriteLine("Number of Vehicles  available for Road :-" + 
           availableVechiecleCounts);
            Console.WriteLine("Number of Vehicles  available for Air :-" + 
            roadTransport.ReturnVechiecleCounts("Air"));
            Console.ReadKey();
        }
    }     
                
Image is not available

Inheritance Implementation :- MultiLevel Inheritance

                
 public class RoadTransport : Transport
    {
        public string VechiecleNumber;
        public string NameOfDriver;
        public string DriverContactNumber;
        public int CapacityOfVechiecle;
        public int ReturnVechicleCapacity(string VechiecleNumber)
        {
            int capacity = 0;
            if(VechiecleNumber=="MP09 UT23**")
            {
                capacity = 45;
            }
            if (VechiecleNumber == "MP09 ZM12**")
            {
                capacity = 55;
            }
            if (VechiecleNumber == "MP09 PL3**8")
            {
                capacity = 30;
            }
            return capacity;
        }
    }           
                
Image is not available
                
  public class Driver : RoadTransport
    {
        public int SupervisorId;
        public string SupervisorName;
        public int RouteNumber;
        public string timings;
        public string LicenseNumber;
    }          
                
                
 public class Transport
    {
        public string DepartmentId;
        public string Head;
        public string Location;
        public string TypesOfTransport;
        public int NumberOfVechiecles;

        public int ReturnVechiecleCounts(string TypesOfTransport)
        {
            int availableVechiecleCounts=0;

            if(TypesOfTransport=="Road")
            {
                availableVechiecleCounts = 15;
            }
            if (TypesOfTransport == "Air")
            {
                availableVechiecleCounts = 2;
            }
            if (TypesOfTransport == "Train")
            {
                availableVechiecleCounts = 6;
            }
            return availableVechiecleCounts;
        }
    }           
                
                
 internal class Program
    {
        static void Main(string[] args)
        {
           Driver objDriver = new Driver();
           int availableVechiecleCounts = objDriver.ReturnVechiecleCounts("Road");
           Console.WriteLine("Capacity of MP09 UT23** is :-" +
                    objDriver.ReturnVechicleCapacity("MP ** UT23**"));
            Console.WriteLine("Capacity of MP09 PL3**8 is :-" + 
                    objDriver.ReturnVechicleCapacity("MP09 PL3**8"));
            Console.WriteLine("Number of Vechiecles available for Road :-" + 
                    availableVechiecleCounts);
            Console.WriteLine("Number of Vechiecles available for Air :-" + 
                    objDriver.ReturnVechiecleCounts("Air"));
            Console.ReadKey();
        }
    }     
                
Image is not available

Image is not available

About the Author
Sudheer Singh Chouhan is a Software Engineer having Expertise in Development Design and Architecting the applications , Project Management , Designing Large Scale Databases in SQL Server since last 17 Years.
Skill Sets :- Microsoft .NET technologies like ASP.Net Core, Web API, LINQ, Web Forms, WinForms, SQL Server, EntityFramework, Design Patterns, Solid Principles, Microservices, AWS Cloud.