C# Inheritance with Examples

In c#, Inheritance is a one of the primary concept of object oriented programming (OOP) and it is used to inherit the properties from one class (base) to another (child) class.

 

The inheritance will enable us to create a new class by inheriting the properties from other classes to reuse, extend and modify the behaviour of other class members based on our requirements.

 

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

C# Inheritance Syntax

Following is the syntax of implementing an inheritance to define a derived class that inherit the properties of base class in c# programming language.

 

<access_modifier> class <base_class_name>

{

    // Base class Implementation

}

 

<access_modifier> class <derived_class_name> : <base_class_name>

{

    // Derived class implementation

}

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

 

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

 

public class X

{

    public void GetDetails()

    {

        // Method implementation

    }

}

public class Y : X

{

    // your class implementation

}

class Program

{

    static void Main(string[] args)

    {

        Y y = new Y();

        y.GetDetails();           

    }

}

If you observe above example, we defined a class “X” with method called “GetDetails” and the class “Y” is inheriting from class “X”. After that, we are calling a “GetDetails” method by using an instance of derived class “Y”.

 

In c#, it’s not possible to inherit the base class constructors in derived class and the accessibility of other members of base class is also depends on the access modifiers which we used to define those members in base class.

C# Inheritance Example

Following is the example of implementing an inheritance by defining a two classes in c# programming language.

 

using System;

 

namespace Tutlane

{

    public class User

    {

        public string Name;

        private string Location;

        public User()

        {

            Console.WriteLine("Base Class Constructor");

        }

        public void GetUserInfo(string loc)

        {

            Location = loc;

            Console.WriteLine("Name: {0}", Name);

            Console.WriteLine("Location: {0}", Location);

        }

    }

    public class Details : User

    {

        public int Age;

        public Details()

        {

            Console.WriteLine("Child Class Constructor");

        }

        public void GetAge()

        {

            Console.WriteLine("Age: {0}", Age);

        }

    }

    class Program

    {

        static void Main(string[] args)

        {

            Details d = new Details();

            d.Name = "Suresh Dasari";

            // Compile Time Error

            //d.Location = "Hyderabad";

            d.Age = 32;

            d.GetUserInfo("Hyderabad");

            d.GetAge();

            Console.WriteLine("\nPress Any Key to Exit..");

            Console.ReadLine();

        }

    }

}

If you observe above example, we defined a base class called “User” and inheriting all the properties of User class into a derived class called “Details” and we are accessing all the members of User class with an instance of Details class.

 

If we uncomment the commented code, we will get a compile time error because the Location property in User class is defined with a private access modifier and the private members can be accessed only within the class.

 

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

 

C# Inheritance Example Result

 

If you observe above result, we are able to access all the properties of base class into child class based on our requirements.

 

 In c#, Structures won’t support inheritance, but they can implement interfaces.

C# Multi Level Inheritance

Generally, c# supports only single inheritance that means a class can only inherit from one base class. However, in c# the inheritance is transitive and it allows you to define a hierarchical inheritance for a set of types and it is called a multi-level inheritance.

 

For example, suppose if class C is derived from class B, and class B is derived from class A, then the class C inherits the members declared in both class B and class A.

 

public class A

{

// Implementation

}

public class B : A

{

// Implementation

}

public class C : B

{

// Implementation

}

If you observe above code snippet, the class C is derived from class B, and class B is derived from class A, then the class C inherits the members declared in both class B and class A. This is how we can implement multi-level inheritance in our applications.

C# Multi Level Inheritance Example

Following is the example of implementing multi-level inheritance in c# programming language.

 

using System;

 

namespace Tutlane

{

    public class A

    {

        public string Name;

        public void GetName()

        {

            Console.WriteLine("Name: {0}", Name);

        }

    }

    public class B : A

    {

        public string Location;

        public void GetLocation()

        {

            Console.WriteLine("Location: {0}", Location);

        }

    }

    public class C : B

    {

        public int Age;

        public void GetAge()

        {

            Console.WriteLine("Age: {0}", Age);

        }

    }

    class Program

    {

        static void Main(string[] args)

        {

            C c = new C();

            c.Name = "Suresh Dasari";

            c.Location = "Hyderabad";

            c.Age = 32;

            c.GetName();

            c.GetLocation();

            c.GetAge();

            Console.WriteLine("\nPress Any Key to Exit..");

            Console.ReadLine();

        }

    }

}

If you observe above example, we implemented a three classes (A, B, C) and the class C is derived from class B, and class B is derived from class A.

 

By implementing a multi-level inheritance, the class C can inherits the members declared in both class B and class A.

 

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

 

C# Multilevel Inheritance Example Result

C# Multiple Inheritance

As discussed, c# supports only single inheritance that means a class can only inherit from one base class. In case, if we try to inherit a class from multiple base classes, then we will get a compile time errors.

 

For example, if class C is trying to inherit from Class A and B at the same time, then we will get a compile time error because multiple inheritance is not allowed in c#.

 

public class A

{

// Implementation

}

public class B

{

// Implementation

}

public class C : A, B

{

// Implementation

}

If you observe above code snippet, the class C is trying to inherit a properties from both class A and B at the same time which will lead to compile time error like “Class C cannot have a multiple classes: A and B”.

 

As discussed, multi-level inheritance is supported in c# but multiple inheritance is not supported. In case, if you want to implement multiple inheritance in c#, that we can achieve by using interfaces. In next chapters, we will learn how to use interfaces to achieve multiple inheritance in detailed manner.

Topics Covered