Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should one interface inherit another interface

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 too 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!

like image 256
ajrawson Avatar asked Nov 06 '09 16:11

ajrawson


People also ask

Can one interface inherit another interface?

An interface can extend any number of interfaces but one interface cannot implement another interface, because if any interface is implemented then its methods must be defined and interface never has the definition of any method.

Can we inherit one interface to another interface Java?

Implements denotes defining an implementation for the methods of an interface. However interfaces have no implementation so that's not possible. An interface can however extend another interface, which means it can add more methods and inherit its type.

Does inheritance apply to interfaces?

An interface contains variables and methods like a class but the methods in an interface are abstract by default unlike a class. Multiple inheritance by interface occurs if a class implements multiple interfaces or also if an interface itself extends multiple interfaces.

Can interface implement other interfaces?

An interface can extend other interfaces, just as a class subclass or extend another class. However, whereas a class can extend only one other class, an interface can extend any number of interfaces.


2 Answers

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 using 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 
like image 145
Jeff Sternal Avatar answered Sep 19 '22 22:09

Jeff Sternal


Consider whether the interfaces should be logically paired, and if you feel that they work well with each other then absolutely use inheritance.

Lets look at an example;

public interface IScanner {     void Scan(); }  public interface IPrinter {     void Print(); } 

Printers and scanners are often separate objects, each with their own functionality however these two devices are often paired in the same device;

public interface IPhotocopier : IScanner, IPrinter {     void Copy(); } 

It makes sense that IPhotocopier should inherit from IScanner and IPrinter as this now allows the photocopier to be used as either a scanner or printer (which it contains) in addition to its primary roll as a copier.

Now lets look at one more interface;

public interface IBlender {     void Blend(); } 

It would not make sense to allow IBlender to be inherited by any of the earlier interfaces (what would you call them? IBlendingScanner?).

If you can't give your new interface a sensible name this might indicate that your may not want to use inheritance in this instance.

It's a bad idea to inherit some interfaces such as IDisposable, since this forces all implementations of your new interface to implement the dispose pattern even if they do not have any disposable resources.

like image 34
mark_h Avatar answered Sep 21 '22 22:09

mark_h