159

I want to know if a class can inherit from a class and an interface. The example code below doesn't work but I think it conveys what I want to do. The reason that I want to do this is because at my company we make USB, serial, Ethernet, etc device. I am trying to develop a generic component/interface that I can use to write programs for all our devices that will help keep the common things (like connecting, disconnecting, getting firmware) the same for all of our applications.

To add to this question: If GenericDevice is in different project, can I put the IOurDevices interface in that project then then make the USBDevice class implement the interface if I add a reference to the first project? Because would like to just reference one project and then implement different interfaces depending on what the device is.

class GenericDevice
{
   private string _connectionState;
   public connectionState
   {
      get{return _connectionState; }
      set{ _connectionState = value;}
   }
}

interface IOurDevices
{
   void connectToDevice();
   void DisconnectDevice();
   void GetFirmwareVersion();
}

class USBDevice : IOurDevices : GenericDevice
{
   //here I would define the methods in the interface
   //like this...
   void connectToDevice()
   {
       connectionState = "connected";
   }
}

//so that in my main program I can do this...

class myProgram
{
   main()
   {
      USBDevice myUSB = new USBDevice();
      myUSB.ConnectToDevice;
   }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
PICyourBrain
  • 9,976
  • 26
  • 91
  • 136
  • 5
    For your future reference, section 10.1.4 of the C# specification describes precisely how to declare a class that has multiple base types. – Eric Lippert Jan 13 '10 at 19:46
  • @Eric Lippert : Could you please help me to understand about this case whether it is correct way and will be available in future? – Gul Ershad May 26 '16 at 06:52

4 Answers4

297

Yes. Try:

class USBDevice : GenericDevice, IOurDevice

Note: The base class should come before the list of interface names.

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.

Zain Rizvi
  • 23,586
  • 22
  • 91
  • 133
Mehrdad Afshari
  • 414,610
  • 91
  • 852
  • 789
  • 1
    Yup this works! Why didn't I think of that! And to the comments below. Thank you for clearing me up on that (Classes don't inherit interfaces, the IMPLEMENT interfaces) – PICyourBrain Jan 13 '10 at 19:10
  • 1
    @Jordan, also note that the base class and the list of interfaces being inherited are separated by commas after the initial colon (@Mehrdad's example). – JMD Jan 13 '10 at 19:10
  • 1
    to expand just a little: if your base class implements the interface then your derived class automatically implements that interface--even without `USBDevice : IOurDevice`. Adding the implementation explicitly does not impact the base class, but it can help put emphasis on the interface. – STW Jan 13 '10 at 19:11
  • 1
    @David, while you're not wrong, it wasn't terminology that was preventing @Jordan's code from working. It was incorrect syntax. – JMD Jan 13 '10 at 19:13
  • Yoooder - If I did that, wouldn't my base class then have to know exactly how a USBDevice works? – PICyourBrain Jan 13 '10 at 19:13
  • @Jordan S: If the base class can be used as the base class for things that don't implement `IOurDevice`, then it shouldn't implement it and the current design is OK. If all `GenericDevice` instances are also `IOurDevice`, you should move the interface implementation to the base class. Depending on the use case, one might be more desirable. Of course, if the base class implements the interface directly, it should provide implementation for all of its members too (or declare them as `abstract`, but that's another story). – Mehrdad Afshari Jan 13 '10 at 19:17
  • 2
    +1 for also clearing up a question I wanted to ask regarding identical members in both the Base and the Interface. – Riegardt Steyn Jun 08 '12 at 08:20
24

No, not exactly. But it can inherit from a class and implement one or more interfaces.

Clear terminology is important when discussing concepts like this. One of the things that you'll see mark out Jon Skeet's writing, for example, both here and in print, is that he is always precise in the way he decribes things.

David M
  • 71,481
  • 13
  • 158
  • 186
23

Unrelated to the question (Mehrdad's answer should get you going), and I hope this isn't taken as nitpicky: classes don't inherit interfaces, they implement them.

.NET does not support multiple-inheritance, so keeping the terms straight can help in communication. A class can inherit from one superclass and can implement as many interfaces as it wishes.


In response to Eric's comment... I had a discussion with another developer about whether or not interfaces "inherit", "implement", "require", or "bring along" interfaces with a declaration like:

public interface ITwo : IOne

The technical answer is that ITwo does inherit IOne for a few reasons:

  • Interfaces never have an implementation, so arguing that ITwo implements IOne is flat wrong
  • ITwo inherits IOne methods, if MethodOne() exists on IOne then it is also accesible from ITwo. i.e: ((ITwo)someObject).MethodOne()) is valid, even though ITwo does not explicitly contain a definition for MethodOne()
  • ...because the runtime says so! typeof(IOne).IsAssignableFrom(typeof(ITwo)) returns true

We finally agreed that interfaces support true/full inheritance. The missing inheritance features (such as overrides, abstract/virtual accessors, etc) are missing from interfaces, not from interface inheritance. It still doesn't make the concept simple or clear, but it helps understand what's really going on under the hood in Eric's world :-)

STW
  • 44,917
  • 17
  • 105
  • 161
  • 5
    Though, unfortunately, interfaces *inherit* other interfaces. I find that choice of words unfortunate, but we're stuck with it now. I prefer to think of interfaces as *requiring* other interfaces. That is, when you say "interface IFoo : IBar" that means "an implementor of IFoo is required to also implement IBar". – Eric Lippert Jan 13 '10 at 19:44
  • @Eric I agree it's an unclear term and debated it with a colleague a while back. In the end we decided that saying "ITwo inherits IOne". I'll update my answer with a couple small reasons (just because they won't fit clearly in a comment). – STW Jan 13 '10 at 20:01
  • Sure; if you define "A inherits from B" as meaning "members of B are all members of A", then interfaces do "inherit" from base interfaces. This is a reasonable definition. But I prefer to think of "inheritance" as being more strictly about not just sharing the abstract, unimplemented members, but rather, being about inheriting *implementations*. Since interfaces have no implementations, I find it somewhat disquieting to think of interfaces as inheriting from anything. It's a subtle and debatable point. – Eric Lippert Jan 13 '10 at 22:07
  • Another term I like is "extend". So you could read "interface IFoo : IBar" to mean that IFoo extends IBar's requirements. – jasonh Jan 13 '10 at 23:06
  • @Eric totally in agreement about it being a moot point to argue... thinking about whether or not the base class has an implementation would put abstract classes or methods in the same ballpark as interfaces – STW Jan 14 '10 at 00:39
  • @Jason to me "extends" is the java keyword for inheritance, although I do like the implied sense that extending something doesn't mean overriding--so it is perhaps a better term for interface inheritance since a subinterface can't override methods in the superinterface (not that overriding a non-implementation would have any effect). – STW Jan 14 '10 at 00:40
  • Yeah, I don't want to introduce confusion, but I think the concept communicates the meaning perfectly. – jasonh Jan 14 '10 at 02:19
  • @Eric: I always hear the same word inheriting for interfaces at work for C# like "DataList inherits IEnumerable", but I always correct them by saying "DataList implements IEnumerable". Is it not accurate? I thought a class can inherit from only a single class, but can implement multiple interfaces. – Joan Venge Jan 14 '10 at 18:51
  • 1
    @Joan: you are correct that *classes* implement (and cannot inherit) interfaces. The point Eric makes is that *interfaces* can inherit other interfaces--which can be somewhat hard to digest given that interfaces are only "specifications" rather than implementations. – STW Jan 14 '10 at 19:25
  • @STW: If one accepts that substitutability is the primary feature of inheritance, derived classes inherit base classes (if all references to the base type, outside of some particular derived class's inheritance qualifier were replaced by references to that derived class, program operation--outside Reflection--would generally be unaffected). Likewise if one replaced all references to an interfaces with references to another which inherited the first but didn't add any members. One could not use such substitution to replace an interface with a class, however. – supercat Jul 09 '12 at 18:51
1

I found the answer to the second part of my questions. Yes, a class can implement an interface that is in a different class as long that the interface is declared as public.

PICyourBrain
  • 9,976
  • 26
  • 91
  • 136
  • It doesn't have to be public in all cases. Just accessible. For example `class ContainsAll { private interface INested { /* ... */ } private class MyExample : INested { /* ... */ } }`, the `MyExample` class implements a nested private interface. In other examples, the nested interface (and the containing class) could be `internal`. It all depends on who needs to use them and bother with them. – Jeppe Stig Nielsen Sep 24 '13 at 15:17