Arun
Arun

Reputation: 3456

C# Deriving a class from more than one abstract class

I created two abstract classes and tried to create a class that inherits from both. But I get an error message.

abstract class AbstractClassOne
{
    public abstract void ShowMessage();
    public abstract void DisplayName();
}

abstract class AbstractClassTwo
{
    public abstract void ShowMessage();
    public abstract void DisplayPlace();
}

class DerivedClass : AbstractClassOne, AbstractClassTwo // here under AbstractClassTwo it shows the error "cannot have multiple base classes:"
{

}

So a class can only derive from one abstract class?

If can derive from more than one abstract class, then what happens if both classes define the same method, as is the case above (abstract class one and two both have a method showmessage(), so which one will be in the derived class)?

Upvotes: 1

Views: 3860

Answers (4)

Mortalus
Mortalus

Reputation: 10712

Multiple inheritance is not allowed by C# but it is allowed by C++. To answer your question regarding the ShowMessage() method that is a known problem in c++ with multiple inheritance called "The Diamond Problem". see: http://en.wikipedia.org/wiki/Multiple_inheritance

So basically you will have to excitability state to which method you are refereeing when calling it e.g. ParentA::ShowMessage()

if you want to have a type that is polymorphic to 2 other types than you should create two separate interfaces and implement them. and if you want to reuse the same methods than you will have to use compositions.

Interfaces example:

public interface ISomeInterface
{
    public void ShowMessage();
    public void DisplayName();
}
public class ClassOne : ISomeInterface
{
    public void ShowMessage()
    {
       //implementation
    }

    public void DisplayName()
    {
       //implementation
    }
}

public class ClassTwo : ISomeInterface
{
    public void ShowMessage()
    {
       //implementation
    }

    public void DisplayPlace()
    {
       //implementation
    }
}

Interface with reusable Show Message Method using composition:

public class ClassTwo : ISomeInterface
{
    private ISomeInterface _MyPrivateReusableComponent = new ClassOne();

    public void ShowMessage()
    {
       _MyPrivateReusableComponent.ShowMessage()
    }

    public void DisplayPlace()
    {
         _MyPrivateReusableComponent.DisplayName()
        //implementation
    }
}

Upvotes: 4

tariq
tariq

Reputation: 2258

No, abstract class whether having all abstract methods or only some, makes no difference as far as inheritance in concerned. you can inherit only one class (in C#) and as many interfaces as you want.

Upvotes: 1

DevT
DevT

Reputation: 4933

In C# it's not allowed to inherit from more than one class. To do what you want here, you need to use interfaces.

abstract class AbstractClassOne
{
    public abstract void ShowMessage();
    public abstract void DisplayName();
}

Interface IClassTwo
{
    void ShowMessage();
    void DisplayPlace();
}

class DerivedClass : AbstractClassOne, IClassTwo
{

}

Upvotes: 4

user1610015
user1610015

Reputation: 6668

You can't inherit from more than one class (abstract or otherwise), but in your case the abstract classes are pretty much interfaces, so you can turn them into interfaces and inherit from them (you can inherit from any number of interfaces).

Upvotes: 2

Related Questions