Wednesday, August 31, 2016

Difference between Abstract Class and Interface

What is an Abstract Class?

An abstract class is a special kind of class that cannot be instantiated. it allows other classes to inherit from it. In simple words, if you inherit this class need to implement methods in this class.
Ex: Like in your project you have all the EDIT,DELETE,UPDATE and SELECT methods, if you want maintain to same hierarchies for all the classes then place these methods in abstract class and inherit this class in all your modules.
Real life example of Abstract : Mobile,TV,Watch and human body...... etc.


What is an Interface?

An interface is not a class. An interface has no implementation; Method without body, As one of the similarities to Abstract class, it is a contract that is used to define hierarchies for all subclasses.


Difference between them is
that a class can implement more than one interface but can only inherit from one abstract class. Since C# doesn’t support multiple inheritance, interfaces are used to implement multiple inheritance.

Abstract class  : static methods, main method and constructor.

Interface: no static methods, main method or constructor.

No comments:

Post a Comment