C# Inheritance with Examples

C# Inheritance with Examples

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 speeds up implementation time.

When creating a class, instead of writing completely new data members and member functions, the programmer can designate 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.

In c# inheritance, the class whose members are inherited is called a base (parent) class and the class that inherits the members of base (parent) class is called a derived (child) class.

C# Inheritance Syntax 

<access_modifier> class <base_class_name>
{
        // Base class Implementation
        // methods and fields
}
<access_modifier> class <derived_class_name> : <base_class_name>
{
        // Derived class implementation
        // methods and fields 
}

If you observe above syntax, we are inheriting the properties of base class into child class to improve the code re-usability.

Following is the simple example of implementing an inheritance in c# programming language

C# Inheritance with Examples

using System;
namespace OppsConcept
{
    class Program
    {
        static void Main(string[] args)
        {
            classD D = new classD();
            D.UserName = "Learn Inheritance";
            D.UserLocation = "Gujarat";
            D.UserCity = "Ahmedabad";
            D.UserAge = 25;
            D.GetUserName();
            D.GetUserLocation();    
            D.GetUserAge();
            D.GetUserCity();

            Console.WriteLine("\n Press Any Key to Exit..");
            Console.ReadLine();
        }

        public class classA
        {
            public string UserName;
            public void GetUserName()
            {
                Console.WriteLine("User Name: {0}", UserName);
            }
        }

        public class classB : classA
        {
            public string UserLocation;
            public void GetUserLocation()
            {
                Console.WriteLine("User Location: {0}", UserLocation);
            }
        }

        public class classC : classB
        {
            public int UserAge;
            public void GetUserAge()
            {
                Console.WriteLine("User Age: {0}", UserAge);
            }
        }
        public class classD : classC
        {
            public string UserCity;
            public void GetUserCity()
            {
                Console.WriteLine("User City: {0}", UserCity);
            }
        }
    }
}

If you observe above example, we implemented a four classes (classA, classB, classC,classD) and the class classD is derived from class classC, and the class classC is derived from class classB, and class classB is derived from class classA, Also this type of inheritance is also called as multi-level inheritance.

When we execute above c# program, we will get the result like as shown below.

C# Inheritance with Examples

 

6 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *