C# Multiple Inheritance using Interfaces

Tom Meier 260 Reputation points
2024-04-10T21:13:26.9+00:00

Hi

I was wondering is there a way to implement multiple inheritance where I can pass an interface type to a method and get all the different implementations from the interface. For example like the method below

void CalculatePay(ICalculate _icalc)

{

// here I can have several different pay calculations.

var res= _icalc. CalculateManagerPay;

var res= _icalc. CalculateSupervisorPay;

var res= _icalc. CalculateSecretaryPay;

}

Best Regards !

Developer technologies C#
{count} votes

2 answers

Sort by: Most helpful
  1. Anonymous
    2024-04-11T03:39:29.5733333+00:00

    Hi @Tom Meier , Welcome to Microsoft Q&A,

    There is no way to write multiple inheritance in C# classes.

    What you are after is polymorphism. Polymorphism is another important concept in object-oriented programming, which allows different subclasses of objects to respond differently to the same method. Polymorphism implements the feature of "one interface, multiple implementations", which can increase the flexibility and scalability of the code.

    In C#, polymorphism can be achieved through method override and interface. When a subclass overrides a virtual method of its parent class or a method that implements an interface, these methods can be called through a reference to the parent class or interface, and which method to call is determined based on the actual object type.

    For example:

    class Animal
    {
         public virtual void MakeSound()
         {
             Console.WriteLine("Animal makes a sound.");
         }
    }
    
    class Dog : Animal
    {
         public override void MakeSound()
         {
             Console.WriteLine("Dog barks.");
         }
    }
    
    class Cat : Animal
    {
         public override void MakeSound()
         {
             Console.WriteLine("Cat meows.");
         }
    }
    
    class Program
    {
         static void Main(string[] args)
         {
             Animal animal1 = new Dog();
             Animal animal2 = new Cat();
    
             animal1.MakeSound(); // Output: Dog barks.
             animal2.MakeSound(); // Output: Cat meows.
         }
    }
    

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2024-04-11T16:02:08.4766667+00:00

    your question is not clear.

    .net supports single inheritance. instead of multiple inheritance it allows implementation of multiple interfaces. an interface is a special type that define properties and methods, but does not implement. you can use reflection to get the properties and methods of an interface.

    if a class implements more than one interface, again you can use reflection to get the list of interfaces, and properties of each interface.

    the issue of multiple inheritance is if two inherited type implement the same method, which one is exposed in the new class. interfaces solve by requiring a cast to the interface to access the common property or method from a class instance.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.