What are multiple inheritances in C#?


C# does not support the usage of multiple inheritances, but it can be implemented using interfaces.

The following is an implementation of Inheritance with Interface. Create two interfaces −

public interface BaseOne {
   void display();
}
public interface BaseTwo {
   void display();
}

Now set the interfaces like you set the derived classes,

public class ChildOne : BaseOne, BaseTwo {
   public void display() {
      Console.WriteLine("Child Class!");
   }
}

We will call the child class function as shown in the following code to implement multiple inheritance in C# −

Example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Demo {
   class Program {
      static void Main(string[] args) {
         ChildOne c = new ChildOne();
         c.display();
         Console.ReadKey();
      }
   }

   public interface BaseOne {
      void display();
   }

   public interface BaseTwo {
      void display();
   }

   public class ChildOne : BaseOne, BaseTwo {
      public void display() {
         Console.WriteLine("Child Class!");
      }
   }
}

Updated on: 21-Jun-2020

31 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements