Reputation: 3585
I have two classes :
public class Question : IDisposable, IEquatable<Question>
{
}
public class SessionQuestion : Question, IDisposable, IEquatable<SessionQuestion>
{
}
As Question
inherits IDisposable
and IEquatable
, does SessionQuestion
implicitly inherit those interfaces as well?
Upvotes: 7
Views: 2279
Reputation:
The answer is "Yes" -- interfaces are inherited. However, whether you can omit including interface definitions from a class depends on whether that class wants to have certain interface members as explicit interface implementations.
Let's take this very simple (and silly) example:
public interface IMyInterface
{
void foo();
}
public class A : IMyInterface
{
public void foo()
{
Console.Out.WriteLine("A.foo() executed");
}
}
public class B : A, IMyInterface
{
void IMyInterface.foo()
{
Console.Out.WriteLine("B.foo() executed");
}
}
Since B wants to have the foo() method as an explicit interface implementation, the IMyInterface interface has to be specified as part of its class definition -- even if class A already implements this interface. Inheriting an interface declaration from a base class is not sufficient in this case.
If class B would not have explicit interface implementations, then class B does not need to specify the interface (IMyInterface in this little example) again.
Inheriting an interface from a base class and having some explicit interface implementation for members of that interface can lead to effects which can be surprising at best, and at worst can lead to broken/buggy software.
If you execute the following code sequence:
A obj = new A();
obj.foo();
IMyInterface x = obj;
x.foo();
the output will be
A.foo() executed
A.foo() executed
However, let's now use an object of type B but otherwise keep the code exactly the same:
B obj = new B();
obj.foo();
IMyInterface x = obj;
x.foo();
the output will be somewhat different:
A.foo() executed
B.foo() executed
Why is that so? Remember that class B inherits the implementation of the foo() method from class A. Thus, calling obj.foo()
will still execute the inherited foo() method.
Now, why is then x.foo()
not invoking the foo() implementation provided by class A as well? Or, why is obj.foo()
not invoking the implementation given for foo() in class B?
Because x is a variable of type IMyInterface, thus the explicit interface implementation of the foo() method provided by the object of type B takes precedence when invoking x.foo()
. The variable obj is not of type IMyInterface, hence obj.foo()
will not invoke the explicit interface implementation of the foo() method.
Because of these surprising results it is easy to understand why explicit interface implementations for interfaces which are already implemented by a base class is really a bad idea in most cases. As the saying goes: Just because you can does not mean you should.
Upvotes: 6
Reputation: 151
Of course, you'll still need to implement all the members that the interfaces define. However, if the base class contains a member that matches an interface member, the base class member can work as the implementation of the interface member and you are not required to manually implement it again.
Upvotes: 3
Reputation: 14870
Of course.
In fact, the .NET Framework is not at all a flat class hierachy. Pick a random class and take a look. You'll see that it derives a class that derives a class that..., and so on.
Interfaces are also inherited to grand children, just like normal classes are.
And as for members you have to implement: If you implemented them in Question
, you don't need to implement them in SessionQuestion
, of course.
Upvotes: 3