blog

Home / DeveloperSection / Blogs / Inheritance in C#

Inheritance in C#

Sumit Kesarwani3392 13-May-2013

In this article, I’m trying to explain the concept of inheritance.

One of the most important concepts in object-oriented programming is inheritance. Inheritance allows us to define a class in terms of another class, which makes it easier to create and maintain an application. This also provides an opportunity to reuse the code functionality and fast implementation.

When creating a class, instead of writing completely new data members and member functions, the programmer can design that the new class should inherit the members of an existing class. This existing class is called the base class, and the new class is referred to as the derived class.

Syntax:
[Access Modifier] class ClassName : baseclassname
{
}

Inheritance can be classified to 5 types:
1.       Single Inheritance
2.       Hierarchical Inheritance
3.       Multi Level Inheritance
4.       Hybrid Inheritance
5.       Multiple Inheritance

Single Inheritance
when a single derived class is created from a single base class then the inheritance is called as single inheritance.

Hierarchical Inheritance
when more than one derived class is created from a single base class, then that inheritance is called as hierarchical inheritance.

Multi Level Inheritance
when a derived class is created from another derived class, then that inheritance is called as multi level inheritance.

Hybrid Inheritance
Any combination of single, hierarchical and multi level inheritances is called as hybrid inheritance.

 Multiple Inheritance
when a derived class is created from more than one base class then that inheritance is called as multiple inheritance. But multiple inheritances are not supported by .net using classes and can be done using interfaces.

Example

using System;
namespaceInheritenceConsoleApplication
{
    classBaseClass
    {
        /// <summary>
        /// create a base class and a method in it
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        publicvoidadd(int x, int y)
        {
            Console.WriteLine("Addition : " +(x+y));
        }
    }
    classDerivedClass : BaseClass//inherit base class
    {
        /// <summary>
        /// create a derived class and a method in it
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        publicvoidmultiply(int x, int y)
        {
            Console.WriteLine("Multiply ; "+(x*y));
        }
    }
    classProgram
    {
        staticvoidMain(string[] args)
        {
            DerivedClassdc = new DerivedClass(); // create a derived class object
            dc.add(21,34); // call base class method with derived class object
            dc.multiply(4,5);
        }
    }
}

 

Output :

Addition : 55

Multiply : 20

In this example, I have created two classes – one base class with method add() and derived class with method multiply(), derived class inherits the base class , in class program – create the derived class object and call the base class method add()  and derived class method multiply() with it and see the desired output.


Updated 18-Sep-2014

Leave Comment

Comments

Liked By