Join Dotnetcodes DotnetCodes.com is online Discussion Forum for Software professionals . It lets you find friends around the world and Create professional network that share similar interests as you. Get help on ur projects by industry specialists. Also get answers to all ur technical/placement related querries.Get an edge over others.
Already MemberClick here to login
ASP.net MVC Interview Questions Answers Interview Questions
Get Started Developing for Android Apps with Eclipse Articles
Whats New in ASP.NET 4.0 Part 2 Articles
.Net framework 4.0 Interview Questions Answers Interview Questions
SQL server reporting services Interview Questions (SSRS) part 1 Articles
SharePoint 2010 interview Questions Answers Interview Questions
Html5 interview questions and answers Interview Questions
Serial Number in SSRS Articles
How to Print a Crystal Report direct to printer Articles
Explaining SDLC -System Development Life Cycle Articles
Populate or bind Dropdownlist in Asp.net using Jquery and Json Articles
Sql Server Database Backup and Restore through C# Source Codes
ASP .Net Page Life cycle Articles
Benefits of Generics in .net Articles
.Net framework 4.0 major improvements Articles
Submit Articles | More Articles..

Abstract Classes vs. Interfaces

Posted By: rakesh On:9/6/2010 7:28:49 AM in:Articles Category:CSharp Hits:2982
The choice of whether to design your functionality as an interface or an abstract class can sometimes be a difficult one. In This article you learn how Abstract Class is differ than Interfaces and How to decide whether to use an interface or an abstract class

Abstract Class

An abstract class is a class that cannot be instantiated, but must be inherited from. An abstract class may be fully implemented, but is more usually partially implemented or not implemented at all, thereby encapsulating common functionality for inherited classes.

Interface

An interface contains only the signatures of methods, delegates or events. The implementation of the methods is done in the class that implements the interface. An interface has no implementation; it only has the signature or in other words, just the definition of the methods without the body. As one of the similarities to Abstract class, it is a contract that is used to define hierarchies for all subclasses or it defines specific set of methods and their arguments. The main 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.

When we use an Interface or an abstract class

Both interfaces and abstract classes are useful for component interaction. If a method requires an interface as an argument, then any object that implements that interface can be used in the argument.

  • f you anticipate creating multiple versions of your component, create an abstract class. Abstract classes provide a simple and easy way to version your components. By updating the base class, all inheriting classes are automatically updated with the change. Interfaces, on the other hand, cannot be changed once created. If a new version of an interface is required, you must create a whole new interface.
  • If the functionality you are creating will be useful across a wide range of disparate objects, use an interface. Abstract classes should be used primarily for objects that are closely related, whereas interfaces are best suited for providing common functionality to unrelated classes.
  • If you are designing small, concise bits of functionality, use interfaces. If you are designing large functional units, use an abstract class.
  • If you want to provide common, implemented functionality among all implementations of your component, use an abstract class. Abstract classes allow you to partially implement your class, whereas interfaces contain no implementation for any members.
comments powered by Disqus
User Profile
Rakesh Sinha
Team Lead
Delhi , INDIA
Email :You must Log In to access the contact details.
Latest Post from :rakesh
What New in ASP.NET MVC 5
View: 446 | Submitted on: 6/6/2014 12:52:30 AM
What is view state in .Net?
View: 745 | Submitted on: 4/21/2014 9:44:52 PM
Rename database in sql server
View: 303 | Submitted on: 2/6/2014 9:57:16 PM
Format Number With Thousands Separator in Sql server
View: 483 | Submitted on: 1/16/2014 3:16:21 AM
Learn How to call Web API with JavasScript and jQuery
View: 577 | Submitted on: 12/18/2013 11:40:49 PM
Submit Articles | All Post of This User..

All rights reserved to dotnetcodes. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks.
Best viewed at 1024 x 768 resolution with Internet Explorer 5.0 or Mozila Firefox 3.5 or Google Crome and higher