19

One interviewer has asked me the below question and I couldn't answer:

Why do we need Interfaces when abstract classes exist?

Whatever the methods we are writing in interface those we can write in Abstract class also. Then why do we need interfaces separately?

Can anybody please tell what is the reason?

Advance thanks...

Ben Reich
  • 16,222
  • 2
  • 38
  • 59
Ashok kumar
  • 1,593
  • 4
  • 33
  • 68
  • 6
    multiple inheritance springs to mind, you can implement multiple interfaces but can only inherit one class in c# – Nicholas King May 29 '13 at 16:04
  • 2
    From Java perspective , abstract classes are for inheritance hierarchy , interfaces for implementation contract . – AllTooSir May 29 '13 at 16:04
  • 2
    Interfaces are also invaluable in Dependency Injection and Unit Testing scenarios. Abstract base classes can still contain logic, and as a result cannot be fully mocked out in testing scenarios. – mclark1129 May 29 '13 at 16:06
  • 1
    Interfaces are adjectives, classes are nouns. – Tim Schmelter May 29 '13 at 16:07
  • 3
    -1, no research effort. You couldn't maybe start by searching on differences between abstract classes and interfaces? It's not exactly an undiscussed topic. – Anthony Pegram May 29 '13 at 16:08

5 Answers5

18

There are several differences,

  • Abstract classes can have only one parent class, whereas a class can implement several interfaces.
  • Interfaces cannot contain any implementation, abstract classes can (they can have abstract methods in addition to not abstract methods).

Interfaces are great to focus on a 'view' we can have on a class. This view can be shared by multiple classes implementing the interface.

For instance, DataTable implements IListSource and ISerializable. So, depending on the context, you can view it as a list source to read its data or as a class which instances can be serialized. When you do so, you focus on a specifc view you can have of an instance.

vc 74
  • 37,131
  • 7
  • 73
  • 89
  • 1
    I'm not fully convinced by this answer.. The language could have been designed to allow multiple inheritance, and it wouldn't obsolete interfaces. Not being able to derive from more than one abstract class isn't a reason to choose an interface over a class or vice versa. – Daniel May 29 '13 at 22:02
  • @Daniel, I agree, I just highlighted one of the most ovious differences – vc 74 May 30 '13 at 06:12
  • @Daniel read about diamond problem multiple inheritance creates. I suppose that is why interfaces exist. – Andrew Shaban May 24 '23 at 19:29
11

Interface represents a contract, while you can have several implementations of that contract in different (abstract) classes.

public interface IExample
{
    void Do();
}

public abstract class DoFirst : IExample
{
    public void Do()
    {
        Console.WriteLine("Doing it the first way");
    }
}

public abstract class DoSecond : IExample
{
    public void Do()
    {
        Console.WriteLine("Doing it the second way");
    }
}

public class DoFirstConcrete : DoFirst, IExample
{
    public void DoSomethingElse()
    {
        Do();
        Console.WriteLine("Doing something else also with first.");
    }
}

public class DoSecondConcrete : DoSecond, IExample
{
    public void DoSomethingElse()
    {
        Do();
        Console.WriteLine("Doing something else also with second.");
    }
}
Janez Lukan
  • 1,439
  • 1
  • 12
  • 28
8

You abstract class is a partial implementation. Interfaces are contracts, to know what your abstract class can do. You need an interface to describe it.

Peter
  • 27,590
  • 8
  • 64
  • 84
5

You can implement multiple interfaces, but only inherit from one abstract class.

An interface is an empty shell, there are only the signatures (name / params / return type) of the methods. The methods do not contain anything. The interface can't do anything. It's just a pattern

Abstract classes, unlike interfaces, are classes. There are more expensive to use because there is a lookup to do when you inherit from them.

Abstract classes look a lot like interfaces, but they have something more : you can define a behavior for them. It's more about a guy saying "these classes should look like that, and they got that in common, so fill in the blanks!".

Quoted e-satis from here (much more information too): What is the difference between an interface and abstract class?

Community
  • 1
  • 1
Fabian Bigler
  • 10,403
  • 6
  • 47
  • 70
0

You can't inherit from multiple abstract classes, but you can implement multiple interfaces.

Robert McKee
  • 21,305
  • 1
  • 43
  • 57