Difference between Abstract Class and Interface in C# Program

CsharpServer Side ProgrammingProgramming

As we all know that C# is an object oriented programming just like Java and provides full support for object-oriented concepts that are Encapsulation, Abstraction, Inheritance, and Polymorphism. In contrast to Abstraction both Abstract class and Interface are coming out in picture as both of these provides abstraction in C# program.

In an abstract class, we can create the functionality and that needs to be implemented by the derived class. The interface allows us to define the functionality or functions but cannot implement that. The derived class extend the interface and implement those functions.

Following are the important differences between Abstract Class and Interface.

Sr. No.KeyAbstract ClassInterface
1DefinitionIn terms of standard definition an Abstract class is, conceptually, a class that cannot be instantiated and is usually implemented as a class that has one or more pure virtual (abstract) functions.On other hand an Interface is a description of what member functions must a class, which inherits this interface, implement. In other words, an interface describes behaviour of the class.
2ImplementationAs like of other general class design in C# Abstract class also have its own implementation along with its declaration.On other hand an Interface can only have a signature, not the implementation. While its implementation is being provided by the class which implements it.
3InheritanceAs per specification in C# a class can extends only one other class hence multiple inheritance is not achieved by abstract class.On other hand in case of Interface a class can implements multiple interfaces and hence multiple inheritance is achieved by interface.
4ConstructorLike other classes in C# for instantiation abstract class also have constructor which provide an instance of abstract class to access its non-static methods.On other hand Interface do not have constructor so we can't instantiate an interface directly although its method could get accessed by creating instance of class which implementing it.
5ModifiersAs abstract class is most like of other ordinary class in C# so it can contain different types of access modifiers like public, private, protected etc.On other hand as Interface needs to be get implemented in order to provide its methods implementation by other class so can only contains public access modifier.
6PerformanceAs abstract class have its method as well as their implementations also for its abstract methods implementation it have reference for its implementing class so performance is comparatively faster as compare to that of Interface.On other hand the performance of interface is slow because it requires time to search actual method in the corresponding class.
raja
Published on 10-Jan-2020 11:15:09
Advertisements