-1

In the past life was easy, interfaces could not have implementations, abstract classes could. Nowadays (like for 5+ years already lol), the only difference between abstract class and interface in C# is that deriving classes can derive from multiple interfaces and they can derive only from one abstract class. Is above statement true or do i miss something?

10
  • 4
    Classes do not "derive from" interfaces, they implement them. So I think the decisive distinction to make here is between "implement" and "derive from". Commented Jan 9 at 9:33
  • 3
    While this is true, IMHO the intention should be the main point in your decision. Adding a default implementation to an interface (while it is possible) should be avoided and from a design perspective an abstract class makes the intention much clearer than an interface with a default implementation. Commented Jan 9 at 9:40
  • 2
    the only difference between abstract class and interface in C# is that deriving classes can derive from multiple interfaces This isn't true. The kind of code you can put into an abstract class is considerably broader than you can get away with on a default interface method. The decision point here for your code is super simple. Is there any common code - use an abstract class. If there isn't - use an interface. Are there edge cases where you can have default code on an interface? Yes, but day to day it is not a useful feature. Commented Jan 9 at 9:41
  • 2
    Thanks for answers, so summing up best practices and @mjwills answer: 1) do not create an interface with the intention of including implementations from the beginning, rather than that add interface implementation in case there is a chance that deriving code (in example DLL's that will not be updated and recompiled) will break in case if interface will be updated 2) interfaces cannot have fields or properties implementations; 3) interfaces cannot implement abstract classes (only extend other interfaces) Commented Jan 9 at 9:47
  • 4
    @sjanisz you're asking about default interface members. This doesn't change how interfaces work, nor is it as new as you assume - Java had them for some time and more importantly, Android SDKs depend on them heavily. That means .NET Core also had to implement them. The way interfaces and abstract classes work hasn't changed. DIM are a way to implement versioning and traits, closer to extension methods, in both languages. And PHP, which has its own traits construct. Check the default-interface-member tag description for clarification and examples Commented Jan 9 at 10:04

2 Answers 2

2

There's a number of differences between interface and abstract class. Abstract classes can:

  1. have virtual members to be overriden in derived classes
  2. enforce initialization data with constructors

Abstract classes are great at combining multiple classes that share a lot of behavior. Interfaces are used to enforce same behavior to unrelated classes.

Default interface implementation can reduce code duplication but you cannot access class' private/protected fields in default implementation.

There are probably more differences but as others have said they have different purposes.

Sign up to request clarification or add additional context in comments.

3 Comments

2. interfaces now can have non-public members too (including private and protected) 4. this also true for interfaces =)
oh, that's cool. Removed it from the list. However, intellisense sees new as a mistake when used with interfaces and advises to remove it. I wonder why it does that
0

The most significant difference is that you cannot call interface methods with default implementation from a class reference that doesn't directly implement the interface.

This is not the case with default implementation provided from an abstract class.

void Main()
{
    Derived derived = new Derived();
    derived.FooBase(); // Abstract
    derived.FooWithImplementation(); // Derived implementation
    
    //derived.FooInterface does not compile
    I interfaceReference = derived;
    interfaceReference.FooInterface(); // Interface
}

interface I
{
    public void FooInterface()
    {
        Console.WriteLine("Interface");
    }

    public void FooWithImplementation()
    {
        Console.WriteLine("Interface implementation");

    }
}

abstract class Base
{
    public void FooBase()
    {
        Console.WriteLine("Abstract");
    }
}

class Derived : Base, I
{
    public void FooWithImplementation()
    {
        Console.WriteLine("Derived implementation");
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.