DEV Community

Cover image for Multiple Inheritance
Abayomi Ogunnusi
Abayomi Ogunnusi

Posted on

Multiple Inheritance

Multiple Inheritance in C

Introduction

This article explains the concept of multiple inheritance in C#. It is a concept of deriving a class from more than one base class.

What is Inheritance?
Inheritance is one of the key features of Object Oriented Programming. It is a mechanism in which one class acquires the property of another class.

⚠️ Note: C# does not support multiple inheritance on classes. However, it supports multiple inheritance on interfaces.

Setup

mkdir MultipleInheritance
cd MultipleInheritance
dotnet new console
Enter fullscreen mode Exit fullscreen mode

In the Program.cs file, add the following code:

namespace MultipleInheritance
{
    class Program
    {
        static void Main(string[] args)
        {
            Student student = new Student();
            student.FirstName = "John";
            student.LastName = "Doe";
            student.CourseName = "Programming with C#";
            student.CourseCode = "C#";
            Console.WriteLine($"Student {student.FirstName} {student.LastName} is enrolled in {student.CourseName} ({student.CourseCode})");

            Teacher teacher = new Teacher();
            teacher.CourseName = "Programming with C#";
            teacher.CourseCode = "C#";

            Console.WriteLine($"Teacher teaches {teacher.CourseName} ({teacher.CourseCode})");
        }

        public interface IPerson
        {
            public string FirstName { get; set; }
            public string LastName { get; set; }

        }

        //ICourse
        public interface ICourse
        {
            public string CourseName { get; set; }
            public string CourseCode { get; set; }
        }

        //Student
        // we cannot inherit from multiple classes but we can inherit from multiple interfaces
        public class Student : IPerson, ICourse
        {
            public string FirstName { get; set; } = string.Empty;
            public string LastName { get; set; } = string.Empty;
            public string CourseName { get; set; } = string.Empty;
            public string CourseCode { get; set; } = string.Empty;
        }

        //Teacher
        public class Teacher : ICourse
        {
            public string CourseName { get; set; } = string.Empty;
            public string CourseCode { get; set; } = string.Empty;
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • We have created two interfaces IPerson and ICourse.
  • We have created a class Student which implements both the interfaces IPerson and ICourse.
  • We have created an object of the class Student and assigned values to its properties.
  • We have printed the values of the properties of the object.

Example II

namespace MultipleInheritance
{
    class Program
    {
        static void Main(string[] args)
        {
            User user = new User();
            user.FirstName = "John";
            user.LastName = "Doe";
            user.Email = "a2b.com";

            Console.WriteLine($"User {user.FirstName} {user.LastName} has email {user.Email}, created at {user.CreatedAt} and updated at {user.UpdatedAt}");
        }

        public interface IBaseEntity
        {
            public int Id { get; set; } 
            public DateTime CreatedAt { get; set; } 
            public DateTime UpdatedAt { get; set; } 
        }

        public interface  IUser : IBaseEntity
        {
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public string Email { get; set; }
            public string Password { get; set; }
        }

        public class User : IUser
        {
            public int Id { get; set; }
            public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
            public DateTime UpdatedAt { get; set; } = DateTime.UtcNow;
            public string FirstName { get; set; } = string.Empty;
            public string LastName { get; set; } = string.Empty;
            public string Email { get; set; } = string.Empty;
            public string Password { get; set; } = string.Empty;
        }


    }
}
Enter fullscreen mode Exit fullscreen mode

Summary

  • Multiple inheritance on classes is not supported in C#.
  • Multiple inheritance on interfaces is supported in C#.

References

Top comments (0)