开发者

Can a class implement two interfaces at the same time?

开发者 https://www.devze.com 2023-01-27 06:24 出处:网络
I tried doing something like this: class Student: IPe开发者_如何学CrsonalDetails: IOtherDetails {

I tried doing something like this:

class Student: IPe开发者_如何学CrsonalDetails: IOtherDetails
{
      //Code
}

It gives error. Why I can't implement two interfaces?


Use a comma between the interface types, e.g.

class Student: IPersonalDetails, IOtherDetails
{
      //Code
}


Change it to

class Student: IPersonalDetails, IOtherDetails
{
  //Code
}


Yes it can, have a deep look at your syntax.


Yes! You definitely can. You can even implement more than 2. I am not sure if there is a limit on how many interfaces you can implement at a time.


Yes, a class can definitely implement more than one interface. After all, that is the whole point of interfaces.

Take a look at the error message you are getting. It is not telling you that a class cannot implement more than one interface. It is telling you that have a syntax error.


Yes, or like this which of course targets completely different design goal and you could say that in fact it's still only one interface due to polymorphic nature of inheritance, but still:

public interface IEntity 
{
    void DoTask();
}

public interface IExtendedTaskEntity : IEntity
{
    void DoExtendedTask();
}

public class ConcreteEntity : IExtendedTaskEntity 
{

    #region IExtendedTaskEntity Members

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

    #endregion

    #region IEntity Members

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

    #endregion
}
0

精彩评论

暂无评论...
验证码 换一张
取 消