C# Inheritance Tutorial For Beginners With Examples

The process through which the properties and behavior of an existing class is inherited by another class is called as Inheritance.
The inheriting class can use the methods of the other class like its own method.

Base And Derived Class

Inheritance is one of the fundamentals of Object Oriented Programming (OOP) along with EncapsulationAbstraction and Polymorphism.
As C# is a Object Oriented Programming Language so it supports Inheritance, Encapsulation, Abstraction and Polymorphism.

Base Class

The class whose properties and behaviors are being inherited.

Derived Class

The class which is inheriting the properties and behaviors of another class.
As a standard, derived class should always be a specialization of the base class.
If there is a base class called as Person then there can be one derived class called as Employeeand another called as Customer.
An Employee is a Person and a Customer is also a Person. Hence, a derived class should always have an "is a" relationship with the base class.
Derived classes can use the members of the base class and can also add members of its own.
    class Entity
    {
        public class Person
        {
            private string allWork;

            public string DoWork(string work)
            {
                allWork = "General work: " + work;
                return allWork;
            }
        }

        public class Employee : Person
        {
            private string specialWork;

            public string DoSpecialWork(string work)
            {
                specialWork = "Special work is " + work;

                return specialWork;
            }
        }

        static void Main(string[] args)
        {
            Employee emp = new Employee();            

            Console.WriteLine(emp.DoWork("Babysitting"));
            Console.WriteLine(emp.DoSpecialWork("Programming"));

            Console.ReadLine();
        }

    }
            
Output:
General work is Babysitting
Special work is Programming
Structs do not support Inheritance.

Virtual Members

The methodsproperties, indexers and event declarations of a base class that are marked as virtual can be overridden in a derived class.
This way the implementation of a base member can be changed in the derived class.
To override a virtual member override keyword is used.
        
    public class Person
    {
        private string allWork;
            
        public virtual string DoWork(string work)
        {
            allWork = "General work: " + work;
            return allWork;
        }
    }

    public class Employee : Person
    {            
        private string allWork;
            
        public override string DoWork(string work)
        {
            allWork = "General employee work: " + work;
            return allWork;
        }         
    }
        

Hiding Base Class Members

The new keyword can be used to hide a member of a base class.
Using new keyword a base class member which is not intended to be overridden can be replaced with the derived class implementation.
    public class Person
    {
        public  void Details()
        {
            Console.WriteLine("Person Details");
        }
    }

    public class Employee : Person
    {
        public new void Details()
        {
            Console.WriteLine("Employee Details");         
        }            
    }
          
    static void Main(string[] args)
    {
        Employee emp = new Employee();

        emp.Details();

        Console.ReadLine();
    }
        
Output: Employee Details

Sealed Classes And Members

The sealed keyword is used to prevent anyone to inherit from a class.
In the examples given in this chapter if sealed modifier is used for the Person class thenEmployee class will not be able to inherit from it.
The sealed keyword can be used as shown below.
    public sealed class Person
    {

    }
        
Members of a class can also be sealed. Doing so will prevent any derived class from overriding that member. However, sealed modifier can only be used with override keyword.
    public class Animal
    {
        public virtual void Color()
        {
            //Method Code
        }
    }

    public class Mammal : Animal
    {
        public sealed override void Color()
        {
            //Method Code
        }
    }

    public class Whale : Mammal
    {
        //Compiler will complain
        public override void Color()
        {
            //Method Code
        }
    }       
        
In the above code Whale class cannot override the Color method of the base class Mammal as it is a sealed method.
The above code is also an example of Multilevel Inheritance.

Abstract Classes and Members

A class declared as abstract cannot be instantiated.
Other classes can inherit from abstract classes and abstract classes can inherit from other classes.
    public abstract class Animal
    {         
        public void Color()
        {
            //Method Code
        }
            
    }
        
Along with regular members, abstract classes can also contain method definitions. These methods do not have a body, abstract keyword is used to define these methods.
Similarly, an abstract class can also have abstract properties. These properties do not provide a concrete implementation.
All abstract methods and properties must be overridden by the derived classes.
    public abstract class Animal
    {
        string _color;

        public void Color(string color)
        {
            _color = color;
            //Rest of the method code goes here
        }

        public abstract void Move(string speed);

        public abstract string Name{get;set;}
            
    }

    public class Mammal : Animal
    {
        string _name;

        public override void Move(string speed)
        {
            //Method code
        }

        public override string Name
        {
            get
            {
                return _name;
            }

            set
            {
                _name = value;
            }
        }
    }
        

Multiple Inheritance

C# does not supports multiple inheritance. Which means we cannot create a class which can inherit from multiple classes.
However, a class can inherit from another class and also an interface simultaneously.
    public interface ICalculate
    {
            int multiply(int x, int y);
    }

    public class Person
    {
        private string allWork;
            
        public virtual string DoWork(string work)
        {
            allWork = "General work: " + work;
            return allWork;
        }
    }

    public class Employee : Person, ICalculate
    {            
        private string allWork;
            
        public override string DoWork(string work)
        {
            allWork = "General employee work: " + work;
            return allWork;
        }

        public int multiply(int x, int y)
        {
            return x * y;
        }
    }