Difference between Abstract class and Interface

Abstract class.

An abstract class in C# is a class which cannot be instantiated. It is declared with abstract modifier. Generally abstract class is used to provide common definition or behavior that the derived classes can implement.

For example

public abstract class Employee

{

}

Interface

An interface in C# is just like a contract that other classes have to follow. It is declared with interface modifier. Interface is used to provide loose coupling between classes and its dependancies.

For example

interface IEmployee

{

}


The difference between abstract class and the interface is as follows.


Abstract Class Interface
Abstract class is declared with abstract modifier An interface is declared with interface modifier
It contains abstract as well as non-abstract methods It contains only abstract methods
You can declare and define the methods in abstract class You can only declare the methods in an Interface
It can have constructor It cannot have constructor
Multiple inheritance cannot be achieved by abstract class Multiple inheritance can be achieved by interface
A class can either implement all of the methods or some methods or none of the methods of the abstract class A class should implement all of the methods of an interface

Comments

Popular posts from this blog

Implicitly typed keyword - var in C#

Classes and objects in C#

Data types in C#