Blue Theme Orange Theme Green Theme Red Theme
 
Mindcracker MVP Summit 2012
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Team Foundation Server Hosting
Search :       Advanced Search »
Home » Visual C# » Inheritance Versus Interfaces

Inheritance Versus Interfaces

This article describes the advantages of using interfaces over inheritance and how to create a plug and play component in .NET using an interface.

Page Views : 32000
Downloads : 0
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
Mindcracker MVP Summit 2012
Become a Sponsor
Mindcracker MVP Summit 2012
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

In object-oriented programming, applications are often designed as complex hierarchies of classes, which are designed to model as closely as possible to the business object we are dealing with. You reuse existing code by inheriting it from an existing base class and overriding its methods. When you derive a subclass from a base class, you must be well aware of the implementation details of the base class. For example, what is the side effect of changing the value of a member variable? How does it affect the code in the base class? Will overriding a base class method and providing a different behavior break the code of clients that expect the base behavior?

This form of reuse is commonly known as white-box reuse, because you are required to be familiar with the details of the base class implementation. White-box reuse simply doesn't allow for economy of scale in large organizations' reuse programs or easy adoption of third-party frameworks.

Interface-oriented programming promotes black-box reuse, which allows you to use an existing component without worrying about its internals implementation, as long as the component complies with some predefined set of operations that are defined in interface.

Thus using Interface-oriented programming we can truely develop a plug and play component for our application. Let us assume we are writing a database application and we are not sure which database our client will be using or our application can be configured to adapt to different databases. Below is the class diagram showing how it can be achieved using interface.



public interface IDataProvider

{

      void AddItem(int itemId, string itemName, string description);

      IDataReader GetItem(int itemId);

      IDataReader GetItems();

      void UpdateItem(int itemId, string itemName, string 

            description);

      void DeleteItem(int itemId);

}

 

public class SqlDataProvider: IDataProvider

{

      public void AddItem(int itemId, string itemName, string

            description)

      {

            // implement the method specific to sql server

      }

 

      public IDataReader GetItem(int itemId)

      {

            // implement the method specific to sql server

      }

 

      public IDataReader GetItems()

      {

            // implement the method specific to sql server

            return null;

      }

 

      public void UpdateItem(int itemId, string itemName, string

            description)

      {

            // implement the method specific to sql server

      }

 

      public void DeleteItem(int itemId)

      {

            // implement the method specific to sql server

      }

}

 

public class OracleDataProvider: IDataProvider

{

      public void AddItem(int itemId, string itemName, string

            description)

      {

            // implement the method specific to oracle

      }

 

      public IDataReader GetItem(int itemId)

      {

            // implement the method specific to oracle

            return null;

      }

 

      public IDataReader GetItems()

      {

            // implement the method specific to oracle

            return null;

      }

 

      public void UpdateItem(int itemId, string itemName, string

            description)

      {

            // implement the method specific to oracle

      }

 

      public void DeleteItem(int itemId)

      {

            // implement the method specific to oracle

      }
}

 

Now in the main method we can get the database value from configuration settings and depending on the database setting we can create the DataProvider of our choice. This model definetly has an benefit over inheritance where our Business object would have largely been dependent on the Database Access layer. Here with this model the client can easily switch between different database or even create there own Data Provider component if new Database has come in market and the component can be easily plugged in the application.

 

static void Main(string[] args)

{

      string database = "SqlServer";

      IDataProvider dataProvider = null;

      if (database.Equals("SqlServer"))

      {

      dataProvider = new SqlDataProvider();

      }

      else if(database.Equals("Oracle"))

      {

      dataProvider = new OracleDataProvider();

      }

}

 

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Dipen Lama
Dipen is a .Net Developer and has arround 5yrs of experience building application in .Net 1.x/2.0/3.0.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the top 5 tips for understanding .NET
Ricky Leeks presents the top 5 tips for understanding .NET Interoperability. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Mindcracker MVP Summit 2012
Become a Sponsor
 Comments
Sealed Classes by srikanth On August 11, 2006

What is Sealed Classes is it related to any Intefaces.

Waht is the main difference of Abstract classes and interfaces.

Can I Implement methods in interfaces,Can i create instance for Interfaces.

Reply | Email | Modify 
Re: Sealed Classes by Martin On March 2, 2007

A selead class is one that cannot be inherited - ever

if you inherit from an abstract class and add your own implementation for the functions etc then any class that inherits from the new class does not need to add its own version of the implementation. With an interface you ALWAYS have to supply an implementation for a new class.

Can I Implement methods in interfaces ? No

Can i create instance for Interfaces ? No

HTH

Martin

Reply | Email | Modify 
thanks by Divya On April 12, 2011
good article.. but its not the continuation of OOPS Concepts and .NET Part 2: Inheritance, Abstraction, & Polymorphism.. I searched for that but didn't get it. thank you..
Reply | Email | Modify 
Discover the top 5 tips for understanding .NET Interop
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.