Wednesday, August 12, 2009

Interface or Abstract Class in C#

I was debating between which would be better for a particular C# class in a project that I'm working on: An Interface or an Abstract Class. So I decided to do some research and highlight the advantages and disadvantages:

  • A class can inherit (implement) multiple Interfaces (+). It can only inherit one Abstract Class (-).
  • Inheritance of an Interface forces the implementation of all parts of the Interface (+).
  • Code might be duplicated using an Interface (-) while it could be written once in an Abstract Class (+).

An Interface is not necessarily better than an Abstract Class. Pick the right tool for the job once you know what the job is.

4 comments:

  1. I use an abstract class if I want to have an established baseline of implementation functionality *and* I want all implementations to inherit that baseline. I.e. "I've done the dirty work, you (inheritors) do the specifics for your own proprietary detail." I use an interface if I am solely concerned with the method and/or property signatures being consistent, i.e. "I say 'Bob', you say 'Bob'! 'Bob!'..'Bob!' 'Bob!'..'Bob!'" :D

    ReplyDelete
  2. I agree with Jon, class inherit abilities from parent. It is used when you want to bring already built implementation down, subclass is going to enrich the functionality or provide logic to handle specifics. Interface on the other side focuses on expressing the mutual agreement among object interaction.
    To be precise, interface isn't in the category of inheritance.
    Another way to say this, parent class serve as a template to the child v.s. interface helps describing the ability the object/class has.
    In the classic object modelling, subclass and interface are used for 2 distinct purpose. As an example, Apple and Orange are special type of fruit. Thus Fruit class is the parent of Apple class and Orange class. Separately, consider there is Phone, Fax and Copier. When you consider an All-In-One machine that provide all 3 functions, You can consider making the All-In-On a class that "implement" the 3 interfaces, Phone interface, Fax Interface and Copier interface.
    But in a lot of cases, a model isn't so obvious, especially when writing procedure based program. Classes and Interfaces are used mostly to organize code and functions into logical and meaningful groups. That's where the line isn't clear between these two. The advantages and disadvantages you described will be the governing rule for deciding the use of either.
    Also a single parent Inheritance is a limitation added into modern language like java or C#. As in the early C++ language you can Inherit from multiple parents. I think the reason that became obsolete is due to the fact that conflicts are difficult to deal with when 2 parent classes define the function with the same signature. It became less efficient for the compiler or runtime environment to support such a "feature".
    This is just my limited understanding of the language features.

    ReplyDelete
  3. By the way, I came across your blog through the "restarting Hyper-V" topic, that is a handy way to deal with the hanging VMs. I have it happened a few times, it appears that 2 of my VMs are doing something in svchost with 100% CPU usage. That made the host machine unresponsive. It made me believe svchost in the host is previding functions to the VMs.
    Since svchost is responsible for a lot of OS functions, it's hard to know exactly what was causing it.
    Do you have any experience with those?

    ReplyDelete
  4. Hi Henry - thanks for your comments. I had not noticed that about the svchost when I was investigating a solution to the 'restarting Hyper-V' problem. I have for the meantime disabled Hyper-V because of problems that it was giving me with my video card and making the machine run very slow. I plan on making that server a remote-into-only server and then I'll switch it back on and I'll take a look at the svchost file if this happens again.

    ReplyDelete