What is the difference between an interface and abstract class?

DWQA QuestionsCategory: C SharpWhat is the difference between an interface and abstract class?
Editor Staff asked 3 months ago

What is the difference between an interface and abstract class?

1 Answers
Editor Staff answered 3 months ago

Interfaces have all the methods having only declaration but no definition. In an abstract class, we can have some concrete methods. In an interface class, all the methods are public. An abstract class may have private methods.
There are some differences between an interface and an abstract class that I have arranged in a table for easier comparison:

ABSTRACT CLASS
INTERFACE

It contains both declaration and definition part.
It contains only a declaration part.

Multiple inheritances are not achieved by an abstract class.
Multiple inheritances are achieved by an interface.

It contains a constructor.
It does not contain a constructor.

It can contain static members.
It does not contain static members.

It can contain different types of access modifiers like public, private, protected, etc.
It only contains public access modifier because everything in the interface is public.

The performance of an abstract class is fast.
The performance of the interface is slow because it requires time to search the actual method in the corresponding class.

It is used to implement the core identity of the class.
It is used to implement the peripheral abilities of the class.

A class can only use one abstract class.
A class can use multiple interfaces.

If many implementations are of the same kind and use common behavior, then it is superior to use an abstract class.
If many implementations only share methods, then it is superior to use Interface.

An abstract class can contain methods, fields, constants, etc.
An interface can only contain methods.

It can be fully, partially or not implemented.
It should be fully implemented.