- Posted On 25 May 2014
- In Programming
This post will explain what is abstract class and interface and when to consider abstract class over interface and vice versa. There are numerous posts on this topic already on other sites but thought sharing the information here will be helpful for the visitors like you who are visiting to read some advanced interview questions and will have some brush-up to this basic but important topic.
In simple words, Abstract class is a class which cannot be instantiated i.e. you cannot create an object of a class when you declare it as an abstract class. Using abstract class you can define some common methods in it and on top you can provide abstract methods which will get overridden by inherited members. This behavior helps you to provide base functionality with a choice of having independent implementation for some functionality.
In above image you can see there is GetPersonalInfo() method is common method for Employee class so it will be available in SoftwareEngineer class and ProjectManager class too as a base functionality but CalculatePerformance() method is defined as abstract which means all the classes inherting from Employee class must provide implementation for this method. With this you have forced inherited members to provide an implementation for CalculatePerformance() method but with a option to have independent logic for each class.
An interface contains only the signatures of methods, properties, events or indexers. A class or struct that implements the interface must implement the members of the interface that are specified in the interface definition. Interface helps us to define a contract i.e. when some class implements a interface it says to outer world that it is capable to provide a functionality defined in interface contract.
You can see in above image Interface forces inherited classes to implement all the methods defined in the interface as a contract. So there is not a base functionality available as like abstract class.
Difference between Abstract class and Interface
- The main difference between abstract class and interface is in abstract class you have a choice of providing base methods with implementation along with abstract methods but with interface all methods are abstract (only signature is there).
- Another difference you can achieve multiple inheritance with interface which is not possible with abstract class i.e. you can inherit from only one abstract class at a time.
When to use Abstract class?
- If you are expecting a multiple versions of your component then consider using abstract class as if abstract class updated all inheriting members automatically gets updated i.e. you can easily add non-abstract methods to abstract class without breaking any code but if you had implemented interface then such addition will break the code as all inheriting member need to implement the new methods.
- If you are designing large functional units which are closely related in hierarchy then consider using abstract classes over interface.
- If you want some base functionality commonly implemented then you can abstract class.
- If you want to decouple the contract from implementation then use abstract class as they are more maintainable.
When to use Interface?
- If you want to provide polymorphic behavior to Value type then you need to use Interface as Value Types must and can inherit from value types only.
- If the functionality you are developing will be useful across a wide range of disparate objects, use an interface.
- If you are designing little, fragment of functionality use interface.
- If you want to achieve an effect similar to that of multiple inheritance, consider using interface.
Hope you have benefited after reading this post. Do share your thoughts in comment section below.
If you want to add some more value to this post do share it too. Thanks.
Typewriter - Automatic TypeScript Template generation from C# classes in Visual Studio
Typewriter, an extension for visual studio that generates TypeScript files from C# code files using TypeScript templates. Convert your C# model classes to TypeScript automatically.
Swagger - Powerful Opensource Framework for RESTful API
With a Swagger-enabled API, you get interactive documentation, client SDK generation and discoverability. Swagger Editor allows you to generate code, documentation with ease.
Visual Studio Code - Lighweight but powerful source code editor available for Windows, OS X and Linux
Assembla - Free and private repository to manage your source code online with SVN subversion hosting
With Assembla you can share source code with others online. Free & Private source code repository with SVN Subversion, Git & Perforce Hosting.
Top 10 Visual Studio things which can boost developers coding speed
Visual Studio 2012 provides some coding features by which you can code faster if use them properly. This post will cover top 10 things among them to boost your development speed.