abstract

What is the difference between Abstract class and Interface?

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.

Interface:

An interface, on the other hand, is not a class which contains only the signature of functionality. It’s a pattern with no implementation. Conceptually speaking, it’s just the definition of methods which contains only the declaration of members. It’s an empty shell which does not contain the implementation of its members. It’s like an abstract base class which only contains abstract members such as methods, events, indexers, properties, etc. It cannot be instantiated directly and its members can be implemented by any class. Additionally, multiple interfaces can be implemented by a class, however, a class can only inherit a single class.

Difference between Interface and Abstarct Class

  Multiple Inheritance – A class can only use one abstract class, hence multiple inheritance is not supported. An interface, on the other hand, can support multiple inheritance, which means a class can inherit any number of inheritances.

  Definition  of Abstract Class and Interface in C#– An abstract class is a special type of class which may contain definition with no implementation. The implementation logic is provided by its derived classes. It can have abstract as well as non-abstract methods. An interface, on the other hand, is just a pattern which cannot do anything. Technically, it’s just an empty shell.

  Implementation – An abstract class can contain both definition and its implementation. It’s an incomplete class which cannot be instantiated. An interface can only have the signature of the functionality without any code.

   Access Modifiers – An abstract class can have several access modifiers such as subs, functions, properties, etc, while an interface is not allowed to have access modifiers and all methods must be implicitly defined as public.

  Homogeneity – An abstract class is used for implementations of the same type, behavior, and status, while an interface is used for implementations that share only method signatures.

  Declaration – An abstract class acts as a base class for all other classes so it can declare or use any variable while an interface is not allowed to declare any variables.

  Constructor Declaration – While an abstract class can have constructor declaration, an interface cannot have constructor declaration.

  Core vs. Peripheral – An abstract class is used to define the core identity of a class and can be used for objects of the same data type. An interface, on the other hand, is used to define the peripheral ability of a class.

  Rigid vs. Supple – An abstract class is more supple in terms of functionality, at least from a developer’s perspective, while an interface is more rigid.

Leave a comment

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