Home > DeveloperSection > Blogs > Inheritance in C#

Inheritance in C#


C# C# 
Ratings:
0 Comment(s)
 1635  View(s)
Rate this:

Inheritance in C#

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;

namespace InheritenceConsoleApplication

{

    class BaseClass

    {

        /// <summary>

        /// create a base class and a method in it

        /// </summary>

        /// <param name="x"></param>

        /// <param name="y"></param>

        public void add(int x, int y)

        {

            Console.WriteLine("Addition : " +(x+y));

        }

    }

    class DerivedClass : BaseClass //inherit base class

    {

        /// <summary>

        /// create a derived class and a method in it

        /// </summary>

        /// <param name="x"></param>

        /// <param name="y"></param>

        public void multiply(int x, int y)

        {

            Console.WriteLine("Multiply ; "+(x*y));

        }

    }

    class Program

    {

        static void Main(string[] args)

        {

            DerivedClass dc = 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.


Don't want to miss updates? Please click the below button!

Follow MindStick