Reputation: 14159
I tried doing something like this:
class Student: IPersonalDetails: IOtherDetails
{
//Code
}
It gives error. Why I can't implement two interfaces?
Upvotes: 5
Views: 1263
Reputation: 7213
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
}
Upvotes: -1
Reputation: 369604
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.
Upvotes: 0
Reputation: 6627
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.
Upvotes: 1
Reputation: 60744
Change it to
class Student: IPersonalDetails, IOtherDetails
{
//Code
}
Upvotes: 9
Reputation: 136717
Use a comma between the interface types, e.g.
class Student: IPersonalDetails, IOtherDetails
{
//Code
}
Upvotes: 19