C# Inheritance

In your programming career you will hear the term Inheritance many times. But the main question is if the concept of Inheritance is clear to you. This article attempts to explain Inheritance. I hope you will get some undersanding of Inheritance. Inheritance is the main concept of OOP. So what is Inheritance? Why do we use it? How to use it? We will discuss these three questions in this article. So the first question is, what is Inheritance?

Inheritance.jpgWhat is Inheritance?

Answer

Inheritance is the most important concept of OOP. Inheritance allows you to reuse existing code. Let me explain that in a simpler way. Suppose there is an automobile company Abcd. This company has been manufacturing cars for 10 years. This company has a famous car Xyz. Now this company wants to manufacture a new model of this car. So what will this company do? Will this company design the entire car again and then manufacture the new model? No, this company will inherit the features of the old car and add the latest features to the new car. With this concept the company saves its time and creates a new model also.

Inheritance1.jpgWhy use Inheritance?


Answer

As I explained above, the main purpose of inheritance is to reuse existing code. Now think about programming, you can do the same thing with code. You can use the existing code feature in your own code. If you want to add some members of the old class in your new class then you can do that with inheritance. As you know, inheritance is an important concept of OOP. Do not take it lightly. If you understand Inheritance very well then you can be a good programmer. You can say that it is a pillar of OOP. If it is strong then OOP is automatically strong. There are 4 pillars of OOP. They are:

  1. Inheritance
  2. Abstraction
  3. Polymorphism
  4. Encapsulation

In this article I explained Inheritance. In the future I will explain the remaining three Pillars. So I hope you now understand why we use Inheritance. Now for the last question.

Inheritance2.jpgHow to use Inheritance?

So the final question is, how to use Inheritance. First note that a class can be derived from more than one class or interface.

The class that is inherited is called the base class and the class that does the Inheritance is called the derived class.

For example,

let us suppose that there is a class named A. You want to inherit the members of Class A in Class B. Then Class A is called the base class and Class B is called the derived class.

The syntax to inherit the class is:

<access-specifier> class <base_class>
{
    ...
}
class <derived_class> : <base_class>
{
    ...
}


Now for something practical. In this code I create a class CDLShape in which I defined two methods, Width and Height. Then I create a class named CDLRectangle that inherits the class CDLShape. Here CDLShape is the base class and CDLRectangle is the derived class.

Code

using System;

namespace CDLInheritance

{

    class CDLShape

    {

        int wdt;

        int height;

        public void Width(int a)

        {

            wdt = a;

        }

        public void Height(int b)

        {

            height = b;

        }

 

        class CDLRectangle : CDLShape

        {

            public int CDLArea()

            {

                return (wdt * height);

            }

        }

        class CDLMain

        {

            static void Main()

            {

                CDLRectangle rect = new CDLRectangle();

                rect.Width(5);

                rect.Height(7);

                Console.WriteLine("Total area " + rect.CDLArea());

                Console.ReadLine();

            }
        }
 

    }

}

The Output is:

Inheritance3.jpg

Final Words

In my next article I will explain Multiple Inheritance in C#. I hope you have learned something from this article. This article is basically for beginners. If you are experienced and you read the complete article, then please add you comments below and tell me what you think about this article. The same things for beginnersy also. Please comment after reading this article.