Difference between Abstract Class and Interface in C#

An Abstract class without any implementation just looks like an Interface; however there are lot of differences than similarities between an Abstract class and an Interface.

What is an Abstract Class?

An abstract class is a special type of class which acts as a base of other classes and cannot be instantiated. The implementation logic of an abstract class is provided by its derived classes. To make a class abstract, the “abstract” modifier is used which means some missing implementation needs to be implemented in the class derived from it. It contains both abstract and non-abstract members. An abstract class is intended to provide basic functionality which can be further shared and overridden by multiple derived classes. It is useful to avoid any kind of code duplication. They look very much like interfaces but with added functionality.

What is an Interface ?

An interface is not a class. It is an entity that is defined by the word Interface. An interface has no implementation; it only has the signature or in other words, just the definition of the methods without the body. As one of the similarities to Abstract class, it is a contract that is used to define hierarchies for all sub classes or it defines specific set of methods and their arguments. The main difference between them is that a class can implement more than one interface but can only inherit from one abstract class. Since C# doesn’t support multiple inheritance, interfaces are used to implement multiple inheritance.


When we create an interface, we are basically creating a set of methods without any implementation that must be overridden by the implemented classes. The advantage is that it provides a way for a class to be a part of two classes: one from inheritance hierarchy and one from the interface.


When we create an abstract class, we are creating a base class that might have one or more completed methods but at least one or more methods are left uncompleted and declared abstract. If all the methods of an abstract class are uncompleted then it is same as an interface. The purpose of an abstract class is to provide a base class definition for how a set of derived classes will work and then allow the programmers to fill the implementation in the derived classes.

Abstract Class vs Interface

Abstract Class Interface
An abstract class can have all access modifiers for member declaration of functions, subs, and properties.An interface is not allowed to have access modifiers for member declaration of functions, subs, properties, etc. All the members are assumed as implicitly public.
A class can at the most use only one abstract class.A class may inherit any number of interfaces.
Multiple inheritance is not supported in abstract class.An interface may support multiple inheritance.
Technically, it’s a class which may or may not contain both definition and its implementation.An interface can only have the signature of functionality so it’s basically an empty shell.
An abstract class can contain members. consts, defined methods, and method stubs.An interface can only contain methods and consts.
It defines the core identity of a class and is used to describe objects of the same data type.It is used to describe the peripheral abilities of a class.
Members of an abstract class cannot be static unless it’s a Complete Member.Members of an interface cannot be static.
It’s ideal for implementations of the same kind and common behavior.It’s better to use interfaces if several implementations share only method signatures.
It can have constructor declaration.It cannot have constructor declaration.
An abstract class has pre-defined fields and constrants.Fields cannot be defined in interfaces.
It can have both abstract and non-abstract methods.It can only have abstract methods.

Summary

While an abstract class is a special type of class which acts as a base for other classes, an interface, on the other hand, is just an empty shell with only member declarations.

0 0 vote
Article Rating

Do you want to hire us for your Project Work? Then Contact US.
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x