Lesson 9 – Inheritance in C#

In C#, you one class can have direct access to the member variables and methods of another class, which is called inheritance. Inheritance is one of the most useful concepts of object-oriented programming where you can implement the code common in different classes in one common parent class. You can then inherit from that parent class to have access to its code. A class that inherits another class is called a child class or derived class, whereas a class that is inherited by other classes is called a parent class or a base class. Inheritance fosters code reusability. In this lesson, you will see inheritance in action.

Simple Inheritance Example

In the last lesson, you saw a C# class named “Computer”; in this example, we will create a class named “SmartPhone” which inherits the “Computer” class which means that the “SmartPhone” class will have access to the member variables and functions of the “Computer” class. The “SmartPhone” class will have one method of its own i.e. “Recharge”. Look at the following example:


namespace C_Project
{
    class Program
    {
        static void Main(string[] args)
        {
            SmartPhone sp = new SmartPhone();
            Console.WriteLine(sp.memory);
            Console.WriteLine(sp.space);
            sp.SwitchOn();
        }

    }


    public class Computer
    {
        public int memory = 32;
        public int space = 500;

        public void SwitchOn()
        {
            Console.WriteLine("Computer has been Switched On");
        }
    }

    public class SmartPhone: Computer
    {
        public void Recharge()
        {
            Console.WriteLine("Recharging Start");
        }
    }
}

To inherit from another class the “:” operator is used. The output of the above script is as follows:

32
500
Computer has been Switched On

Passing Values to Parent Constructor

While inheriting from a class, you can pass values to parent class constructor from a child class. All you have to do is use “:Base” keyword after the child class constructor and pass the values to the parent class constructor within the parenthesis as shown below:

using System;

namespace C_Project
{
    class Program
    {
        static void Main(string[] args)
        {
            SmartPhone sp = new SmartPhone(64, 1500, 55000);
            Console.WriteLine(sp.memory);
            Console.WriteLine(sp.space);
            sp.SwitchOn();
        }

    }


    public class Computer
    {
        public int memory;
        public int space;

        public Computer(int memory, int space)
        {
            this.memory = memory;
            this.space = space;
        }
        public void SwitchOn()
        {
            Console.WriteLine("Computer has been Switched On");
        }
    }

    public class SmartPhone : Computer
    {
        public int price;

        public SmartPhone(int memory, int space, int price) : base(memory, space)
        {
              price = this.price;
        }
        public void Recharge()
        {
          
            Console.WriteLine("Recharging Start");
        }
    }

}



Here is the output for the above script:

64
1500
Computer has been Switched On

What’s Next?

In this lesson you studied inheritance, in the next and final lesson of this course, you will see how to handle exceptions in C#.