Subscribe - It's FREE!!

Stay Connected Here

Stay Updated With Us Here



Google

Abstract Class v/s Interface What is the diffrence and When to use which


Share with WhatsApp


Abstract Class v/s Interface What is the diffrence and When to use which


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.

Abstract Class

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.

Abstract Class

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.

Interface

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.

Interface

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. smiley



If you enjoyed this post take 5 seconds to share it! Be Socialable. :-)

Share with WhatsApp

Posts To Read Next

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.


Visual Studio 2008 Shell and TFS integration

Visual Studio 2008 Shell and TFS integration is the problem for all newbies of BIDS and TFS. Here is the solution.


How to call click or any event only once in jQuery

Know how to execute an click event or any event only once for any element in jQuery. Perform action only once and even not required to unbind event.


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.


Best CSS Gradient background generator tools online

Here are some best CSS gradient background code generator online tools using which you can create a cross browser css code for gradient backgrounds.


Your opinion is valuable for us! Comments, suggetions are welcome.


Submit your Email Id to stay updated with us and get notified with our new posts. It's FREE!
We know this popup is disturbing you!
But We would greatly appreciate if you share us with your friends below!

It will not take more than 2 seconds but will motivate us greatly to write more,share more!

x