Abstract Class and Interface in C#: Difference and example

Abstract class and Interface

What Does “Abstract Class” Mean?

In programming languages, an abstract class is a type of object that is used to create specific objects that follow its protocol, or the set of operations it supports.

In Java, an abstract class is a superclass that can’t be created and is used to describe or define general properties. A Java abstract class can’t be used to make an object. If you try to instantiate an abstract class, you’ll get a compiler error. The keyword abstract is used to declare the abstract class.

Subclasses that are extended from an abstract class have all the properties of the abstract class as well as properties that are unique to each subclass. The abstract class says what the class’s properties are and how they should be implemented. This describes the whole interface.

Abstract classes are useful for making hierarchies of classes that model reality because they let you specify a constant level of functionality in some methods while leaving the implementation of other methods until a specific implementation of that class (a derived class) is needed.

Example of Abstract Class in C#:

program

In the above example, the derived class MyArea overrides the abstract method “Area” in the MyCircle class.

What Does “Interface” Mean?

An interface only has the declarations of methods, properties, events, and indexers. It doesn’t show how these members are put into action. When a class or struct implements an interface, it must define the methods that the interface says it has. That means that an interface gives a derived class a standard way to be put together.

Example of an Interface in C#:

program

The interface MyData includes two method declarations, getdata() and showdata, as illustrated in the sample above (). The definition of the methods declared in the MyData interface should be provided by the class NameData that is implementing it.

Key Difference

Abstract ClassInterface
It contains both declaration and definition part.It contains only a declaration part.
Multiple inheritance is not achieved by abstract class.Multiple inheritance is achieved by interface.
It contain constructor.It does not contain 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 interface is slow because it requires time to search actual method in the corresponding class.
It is used to implement the core identity of class.It is used to implement peripheral abilities of class.
A class can only use one abstract class.A class can use multiple interface.
If many implementations are of the same kind and use common behavior, then it is superior to use abstract class.If many implementations only share methods, then it is superior to use Interface.
Abstract class can contain methods, fields, constants, etc.Interface can only contains methods, properties, indexers, events.
It can be fully, partially or not implemented.It should be fully implemented.