Inheritance vs. interface in C#

45,802

Solution 1

Class inheritance represents an "is-a" relationship, e.g., a Tank is a Vehicle. If your situation doesn't at least meet this, choose interface over inheritance.

If the proposed base class doesn't provide default implementations for your methods, this would be another reason to choose interface over inheritance.

In C#, you can only inherit from one class but multiple interfaces. This would be yet another reason to choose interface over inheritance.

Get the idea?

Solution 2

Inheritance is, in my opinion, the better approach when it is possible. Implementing common features in a base class helps ensure that the underlying implementation is consistent, while implementing an interface guarantees only that the, well, interface is consistent. Inheritance wherever possible is one leg of the OOP tripod, and limits code duplication.

I use interfaces when I have objects that can't or shouldn't have a common base class, but need to be provide similar functionality. Classes that implement a common interface may (and probably do) expose additional functionality, may implement multiple interfaces, etc.

For instance: in one application, my data access layer is built around "provider" classes, which insulate business objects and database proxy objects from the database. In one instance, I have a provider which interacts with a SQL Server database and another for communicating with Oracle CRM On Demand (aka, Why God Why). The both implement an agnostic interface so that the client code doesn't care which data store it's dealing with, or the quirks of working with each.

The Oracle provider communicates with a hosted server via a collection of web services, manages connection sessions, batches requests, etc. The SQL provider uses a set of stored procedures. Although both interfaces accept the same data classes, the Oracle provider transforms the data to match their esoteric (to put it lightly) schema. With this implementation, I could easily add a provider to use an XML data store, a different RDBMS, or a stub/mock unit test.

In this situation, it doesn't make much sense for these things to have a common base class and, in a few instances, it's impossible.

Solution 3

Honestly, do both.

public interface IScraper
{
  string ScrapeDate(string url);
}

public abstract class Scraper : IScraper
{
  public string ScrapeDate(string url)
  {
    // default implementation
  }
}

There's advantages either way, but those are difficult to quantify without knowing more about your requirements. However, there's no reason you can't do both. Having an interface for your class makes it mockable for testing purposes as well.

Something else to consider though; if the functionality for each of your derived classes are similar enough, it may be easier to simply have a single class that takes parameters to the constructor.

Solution 4

An interface contains only the signatures of methods, delegates or events. The implementation of the methods is done in the class that implements the interface.

A class can implement multiple interfaces.

A class can have only one direct base class.

Solution 5

Refering to Abstract Class versus Interface.

There are some similarities and differences between an interface and an abstract class:

A class may implement several interfaces.
A class may inherit only one abstract class.

An interface cannot provide any code, just the signature.
An abstract class can provide complete, default code and/or just the details that have to be overridden.

An interface cannot have access modifiers for the subs, functions, properties etc everything is assumed as public
An abstract class can contain access modifiers for the subs, functions, properties

Interfaces are used to define the peripheral abilities of a class. In other words both Human and Vehicle can inherit from a IMovable interface.
An abstract class defines the core identity of a class and there it is used for objects of the same type.

If various implementations only share method signatures then it is better to use Interfaces.
If various implementations are of the same kind and use common behaviour or status then abstract class is better to use.

If we add a new method to an Interface then we have to track down all the implementations of the interface and define implementation for the new method.
If we add a new method to an abstract class then we have the option of providing default implementation and therefore all the existing code might work properly.

No fields can be defined in interfaces
An abstract class can have fields and constrants defined

Share:
45,802
Hui
Author by

Hui

Updated on April 15, 2020

Comments

  • Hui
    Hui about 4 years

    Possible Duplicates:
    Interface vs Base class
    When should I choose inheritance over an interface when designing C# class libraries?

    So I'm writing my first real program in C#. The program will scrape data from four different websites. My plan is to have one parent class that will look like this:

    class Scraper
    {
        string scrapeDate(url);
        string scrapeTime(url);
        //&c.
    }
    

    Then I will have four classes that inherit from it.

    The other option is to make Scraper an interface and have four classes implementing it.

    What are the differences between these approaches?

  • Thomas Levesque
    Thomas Levesque about 13 years
    Tiens, tu es là toi ? C'est pour ça qu'on te voit plus beaucoup sur Developpez... ;)
  • PhillipKregg
    PhillipKregg over 12 years
    Those are good simple rules of thumb.