-1

The following code give me error that

Error 2 The type 'Series.AB' already contains a definition for 'B'

 interface IA
{
    void A();
}
interface IB
{
    void B();
}

interface IAB : IA, IB
{
    void A();
    void B();
}

class AB : IAB
{
    IA A;
    IB B;
    public AB(IA _a, IB _b)
    {
        A = _a;
        B = _b;
    }

    public void A()
    {
        throw new NotImplementedException();
    }

    public void B()
    {
        throw new NotImplementedException();
    }
}

I thought that I can use AB class instance as

IA A = new AB(); Or IB B = new AB(); Or IAB ab = new AB();

i am not able to understand what happen here. please anyone describe why this exception occurs.

Ankit Rana
  • 383
  • 6
  • 24
  • 4
    Posible duplicate of: http://stackoverflow.com/questions/2371178/inheritance-from-multiple-interfaces-with-the-same-method-name – Rumpelstinsk Aug 25 '16 at 06:37
  • 2
    This error is unrelated to interface and inheritance really. You have a method and a field with the same names. `B` is the name of a field and a method (the same goes for `A`). That is not allowed. – Mike Zboray Aug 25 '16 at 06:38
  • 1
    The closest possible duplicate I found: [Already contains a definition for](http://stackoverflow.com/questions/13770014/already-contains-a-definition-forget-set-method). In either question OP think it's something else. – Sinatr Aug 25 '16 at 06:50
  • Possible duplicate of [How can interfaces replace the need for multiple inheritance when have existing classes](http://stackoverflow.com/questions/5003285/how-can-interfaces-replace-the-need-for-multiple-inheritance-when-have-existing) – Sayali Sonawane Aug 25 '16 at 07:14
  • Interface IA is pointing and capable of refering only A() method ,similarly IB B() method .SO IAB ab should work smoothly.A solid principle is that the type that is inheriting the interface must provide implementation in the entire inheritance chain. – Hameed Syed Aug 25 '16 at 07:44

1 Answers1

1

Change the Property Names to Something other than A and B as you have method names with same name. Also have default constructor in class AB.

class AB : IAB
{
    IA instanceA;
    IB instanceB;
    public AB(IA _a, IB _b)
    {
        instanceA = _a;
        instanceB = _b;
    }

    public void A()
    {
        throw new NotImplementedException();
    }

    public void B()
    {
        throw new NotImplementedException();
    }
}