Skip to main content

Posts

Showing posts from June, 2018

Abstract Class vs Interface in C#

Alot of junior (maybe not only junior) developers has been asked in an interview "what is abstract class and how why do we need it if we have interfaces?". Let's try understand what is the abstract class, when it can be helpful and how it differs from an interface. (Important note: in this article we will talk about classical abstract classes and interfaces. In some languages (like D or new java) some things can be different, so in the post I reference a C# language for examples.) Differences The main differences between abstract classes and interfaces are: Abstract class can specify some implementation, which is common for all inherited classes Interface provides no implementation, only the contract. So you can see that abstract class defines a type hierarchy, and interface defines a contract. You can also see the difference as "A is B" vs "A implements B" . When would you use abstract class. Let's imagine that we are writi...