ʞᴉɯ
ʞᴉɯ

Reputation: 5594

.net Multiple inheritance from abstract classes

it is possible to implement this functionality? I've two abstract classes, and i want to force a third class to implement all the abstract methods in the two classes

public abstract class A
{
    private void MethodA()
    {
        var fool = Prop*2;
    }
    public abstract int Prop { get; }
}
public abstract class B
{
    private void MethodB()
    {
        //.... 
        SomeMethod();
        //....
    }
    public abstract void SomeMethod();
}

public class C: A, B
{
    public int Prop { .... }
    public void SomeMethod { .... }
}

The main problem here is that the implemented final class methods are then used in base abstract classes, and this cannot be achieved with interfaces. So there is no a workaround?

I've read many similar q/a but nothing responding to my problem. Thanks

Upvotes: 11

Views: 37590

Answers (6)

MitsakosGR
MitsakosGR

Reputation: 531

As of C# 8.0 on .NET Core 3.0, C# supports default implementations for Interface members.

public interface ILight
{
    void SwitchOn();
    void SwitchOff();
    bool IsOn();
}

// Basic Implementation
public class OverheadLight : ILight
{
    private bool isOn;
    public bool IsOn() => isOn;
    public void SwitchOff() => isOn = false;
    public void SwitchOn() => isOn = true;

    public override string ToString() => $"The light is {(isOn ? "on" : "off")}";
}

// Interface with default implementation
public interface ITimerLight : ILight
{
    // defines default implementation
    public async Task TurnOnFor(int duration)
    {
        Console.WriteLine("Using the default interface method for the ITimerLight.TurnOnFor.");
        SwitchOn();
        await Task.Delay(duration);
        SwitchOff();
        Console.WriteLine("Completed ITimerLight.TurnOnFor sequence.");
    }
}

// Implementation with interface
public class OverheadLight : ITimerLight
{
    private bool isOn;
    public bool IsOn() => isOn;
    public void SwitchOff() => isOn = false;
    public void SwitchOn() => isOn = true;

    public override string ToString() => $"The light is {(isOn ? "on" : "off")}";

    // Automatically gets the interface implementation
    //
    // To provide your own implementation: (no use of override)
    // public async Task TurnOnFor(int duration)
    // {
    // }
}

Upvotes: 9

Louis Kottmann
Louis Kottmann

Reputation: 16628

You can do this in C# via Mixins, it's implemented differently than multiple inheritance but in the end you do get an object with all the methods from several classes.

It is not a feature of the language, but it can be found in third parties.

You can read about it on my answer to this other question, or directly on re-mix's codeplex

A few points of interest:
- you can override methods in a Mixin, thus externally modifying the behavior of newly created classes.
- you can create scopes where classes are different while created within the scope.
- you need to create mixin'd objects via a factory (new MyObject() won't take Mixins into account, thankfully).

Upvotes: 2

aserwin
aserwin

Reputation: 1050

If you are willing to implement the functionality in the final class, you can create interfaces and implement as many as you want...

Upvotes: 1

Adam Robinson
Adam Robinson

Reputation: 185643

That specifically? No, as multiple inheritance is not supported in C#.

The closest you could get would be using an interface that combined two others:

public interface ISomeInterfaceA
{
    int Prop { get; }
}

public interface ISomeInterfaceB
{
    void SomeMethod();
}

public interface ICombined : ISomeInterfaceA, ISomeInterfaceB
{

}

Then you could require arguments of type ICombined, but this doesn't completely get what you're after. For instance, if I were to do this:

public class A : ISomeInterfaceA, ISomeInterfaceB
{
  ...
}

This wouldn't qualify, since I didn't explicitly implement the combined interface, just the two interfaces that it's composed of.

Upvotes: 1

MikeB
MikeB

Reputation: 2452

Have you tried using multiple interfaces instead? This might be a similar situation: Multiple derived abstract classes?

Upvotes: 1

Claudio Redi
Claudio Redi

Reputation: 68420

No, C# doesn't support multiple inheritance. You could implement multiple interfaces but that's obviously not the same since interfaces can't have any concrete behavior implemented.

Upvotes: 11

Related Questions