views:

282

answers:

4

I can't seem to find an answer on this and just want to make sure it's an ok coding standard. I have Interface A that is used by many different classes and don't want interface A to change. I came across a new requirement that will require an enum to be needed by many of the classes that Implement Interface A, but not all the classes need this enum. I don't want the classes that don't require this new enum to Implement this new functionality. So I created Interface B that contains the new enum that I needed to add. I then made Interface B Inherit Interface A and this is my concern, Is it ok for one Interface to Inherit another Interface? To continue with my changes, I then changed the classes that needed the new enum to Implement Interface B instead of Interface A since it was Inherited by Interface B. I thought about Implementing both Interfaces in my classes that needed them but I'm using the Interface throughout the code and would like to just use one Interface for looking through classes and not two.

I hope this was clear enough (probably to long) but if anyone can give me some advice on this either I'm doing it right or I'm doing it wrong please let me know.

Thanks!

+22  A: 

Interface inheritance is an excellent tool, though you should only use it when interface B is truly substitutable for interface A, not just to aggregate loosely-related behaviors.

It's difficult to tell whether it is appropriate for your specific case, but there's nothing wrong the practice in principle. You see it in the first-rate APIs all the time. To pick just one common example, from the .NET framework:

public interface ICollection<T> : IEnumerable<T>, IEnumerable
Jeff Sternal
I originally suggested the Liskov substitution principle should apply. In retrospect, that isn't really relevant. Most of the requirements of the LSP (maintaining invariants, restrictions on pre- and post-conditions) are really only applicable to concrete implementations, not interfaces. Having said that, the general principle of substitutability should still guide interface inheritance decisions.
Jeff Sternal
+1  A: 

IMO this is exactly the right approach, I don't see any problem with it.

J. Loomis
+4  A: 

It is certainly possible to have an inheritance tree of interfaces, and even "multiple inheritance" with interfaces. Whether it is the right thing to do or not depends on the interfaces in question. If it really is the case that interface B is an extension or refinement of interface A, then inheritance makes sense, but if the new enum is largely unrelated to the concept expressed by interface A, I would make them two separate interfaces and have the classes that need to implement both interfaces.

qid
Good point and yes in this situation they are related.
ajrawson
A: 

Technically speaking, interfaces don't inherit from each other. What really happens when you create an IFoo that inherits from IBar, you're saying that any class that implements IFoo must also implement IBar.

interface IBar
{
    void DoBar();
}

interface IFoo : IBar
{
    void DoFoo();
}

In this example, the IFoo interface does not have a DoBar() method. Most of the time the distinction doesn't matter, but it can bite you when using reflection on an interface rather than a class.

Joel Mueller
Even given the behavior you've described (described in excellent detail in Phil Haack's recent column, http://haacked.com/archive/2009/11/10/interface-inheritance-esoterica.aspx), I think it's unduly confusing to say 'interfaces don't inherit from each other' in C# when the C# reference documentation even uses that terminology, as in "An interface can inherit from one or more base interfaces." (http://msdn.microsoft.com/en-us/library/87d83y5b%28VS.80%29.aspx) I'd prefer just to say that, in C#, interface inheritance behaves differently than class inheritance when reflecting inherited members.
Jeff Sternal
Fair enough. I guess it depends on how "inherit" is defined. Certainly the C# syntax is the same whether it's classes or interfaces. But if you think of inheritance as including the members of the parent, then your mental model doesn't match reality when you're talking about interfaces.
Joel Mueller