C# – Difference between Abstract class and Interface

An Abstract category associate degreey with none implementation just looks like Interface; but there area unit lot of variations than similarities between associate degree Abstract category and an Interface. Let’s explain both concepts and compare their similarities and differences.

What is Abstract Class?

An abstract category could be a special kind of category that cannot be instantiated. that the question is why we need category that cannot be instantiated? associate degree abstract class is barely to be sub-classed (inherited from). In different words, it solely permits different categories to inherit from it however can’t be instantiated. The advantage is that it enforces sure hierarchies for all the subclasses. In simple words, it’s a sort of contract that forces all the subclasses to hold on the same hierarchies or standards.

What is in Interface

An interface isn’t a category. it’s  entity that is outlined by the word Interface. associate degree interface has no implementation; it solely has the signature or in different words, simply the definition of the ways without the body. as one of the similarities to Abstract category, it’s a contract that’s accustomed outline hierarchies for all subclasses or it defines specific set of ways and their arguments. the most distinction between them is that {a category|a category} will implement over one interface however will solely inherit from one abstract class. Since C# doesn’t support multiple inheritance, interfaces are accustomed implement multiple inheritance.

When we produce  interface, we tend to area unit basically creating a set of ways with none implementation that has to be overridden by the enforced categories. The advantage is that it provides how for a category to be an area of 2 classes: one from inheritance hierarchy and one from the interface.

When we produce abstract category, we tend to area unit making a base category which may have one or more completed ways however a minimum of one or more ways square measure left uncompleted and declared abstract. If all the ways of  abstract category area unit uncompleted then it’s same as interface. the aim of  abstract category is to produce a base category definition for how a group of derived categories can work and then enable the programmers to fill the implementation in the derived categories.

Interface

  1. A class may inherit several interfaces.
  2. . An interface cannot provide any code, just the signature.
  3. Interfaces are used to define the peripheral abilities of a class. In other words both Human and Vehicle can inherit from a IMovable interface.
  4. An interface cannot have access modifiers for the subs, functions, properties etc everything is assumed as public

Abstract class

  1. A class may inherit only one abstract class.
  2. An abstract class can provide complete, default code and/or just the details that have to be overridden.
  3. An abstract class can contain access modifiers for the subs, functions, properties
  4. An abstract class defines the core identity of a class and there it is used for objects of the same type

I am try to explained the major differences between an abstract class and an interface.

Leave a Reply

Your email address will not be published. Required fields are marked *