Showing posts with label Interface vs Abstract Class in C#. Show all posts
Showing posts with label Interface vs Abstract Class in C#. Show all posts

14 June 2011

Interface vs Abstract Class in C#

Interface vs Abstract Class

Multiple inheritance

Interfaces:A class may inherit several interfaces.
Abstract Class:A class may inherit only one abstract class.

Default implementation

Interfaces:An interface cannot provide any code, just the signature.
Abstract Class:An abstract class can provide complete, default code and/or just the details that have to be overridden.

Access Modifiers

Interfaces:An interface cannot have access modifiers for the subs, functions, properties etc everything is assumed as public
Abstract Class:An abstract class can contain access modifiers for the subs, functions, properties

Core VS Peripheral

Interfaces:Interfaces are used to define the peripheral abilities of a class. In other words both Human and Vehicle can inherit from a IMovable interface.
Abstract Class:An abstract class defines the core identity of a class and there it is used for objects of the same type.

Usage in Application

Interfaces:If various implementations only share method signatures then it is better to use Interfaces
Abstract Class:If various implementations are of the same kind and use common behaviour or status then abstract class is better to use.

Speed

Interfaces:Requires more time to find the actual method in the corresponding classes.
Abstract Class:Compare to Interface it is very fast

Fields and Constants

Interfaces:No fields can be defined in interfaces
Abstract Class:An abstract class can have fields and constrants defined


Main Limitation of Interface

If we add a new method to an Interface then we have to track down all the implementations of the interface and define implementation for the new method
in all derived class.

Main Limitation of Abstract Class

A class may inherit only one abstract class.

Consistency level in Azure cosmos db

 Consistency level in Azure cosmos db Azure Cosmos DB offers five well-defined consistency levels to provide developers with the flexibility...