Difference Between Abstract class and Interface in C#

abstract class vs interfaceAbstract Class

An abstract class is one that is intended only to be a base class of other classes. The ‘abstract’ modifier is used to make a class abstract. An abstract modifier indicates that there is some missing implementation that needs to be implemented in the class derived from it. The abstract class can have abstract and non-abstract members. An abstract class should have at least one abstract method, otherwise, there is no use of declaring that class as ‘abstract.’

Example of Abstract Class in C#:
abstract classAs shown in the above example, the abstract method ‘Area’ inside the MyCircle class is overridden in the derived class MyArea.

Interface

An interface contains only the declaration of members including methods, properties, events or indexers. It doesn’t contain the implementation of these members. A class or struct implements the interface, must provide the definition of methods declared in the interface. That means an interface provides a standard structure that the derived class needs to follow.

Example of an Interface in C#:
interface programAs shown in the above example the interface MyData has two method declaration getdata() and showdata(). The class NameData who is implementing this interface should give the definition of methods declared in the MyData interface.

Difference Between Abstract class and Interface

  • An abstract class can have abstract as well as non-abstract members. But, in an interface, all the members are implicitly abstract and must be overridden in the derived class.
  • A class may inherit several interfaces but only one abstract class.
  • An abstract class can contain methods with or with a definition. But, an interface can only contain method signature.
  • An abstract class can fully, partially or not implemented at all, but an interface should be fully implemented, i.e., all the members must be defined in the deriving class.
  • An interface cannot contain access modifiers, but an abstract class can have it for functions, properties or events.
  • ncG1vNJzZmijla6xqrLFnqmeppOawG%2BvzqZmnaGWm7Kzsc2cnGaalanEprHNZpibq6SnrqTAjJyjmqujYq6vsIyipa2dopuupLGNoaumpA%3D%3D