What is the Use of interfaces?( .NET Interview questions with answers)

Basically there are two uses

  1. Define Contracts – some time we want to create some kind of rules which we want everybody to follow. In this situation we will use interfaces. With the help of interface we form “Can a” relationship.
    We declare what all functionalities a class can do in interface. Class which implement interface must define those functionalities or else we end up with compile error.
    Hence we say we use contracts because we want to create a contract which everybody should follow.
  2. Loosely coupling – Normally when we say application we have classes which communicate with each other. When one class create object of another class we say, this two classes are tightly coupled.
    Example:

Class CustomerMiddleLayer
{
            public void AddCustomer()
            {
                         CustomerSqlLayer layer=new CustomerSqlLayer();
                         layer.Insert();
            }
}
Class CustomerSqlLayer
{
            public void Insert()
            {
                         //Create Sql Insert query and fire
            }
}

Now let’s say sometime in future user decides to migrate to XML from SQL. In order to achieve this we have to create new CustomerXMLLayer and change CustomerMiddleLayer accordingly. It’s a complete violation of OCP – Open closed principle which says, software module should not allow modifications but can be extended.
            We can remove this kind of tight dependency with the help of interfaces.
            Step 1 – Create an interface as follows
           public interface IDataAccessLayer
           {
                       Void Insert();
           }
           Step 2 – Create CustomerSqlLayer and implement above interface
           Class CustomerSqlLayer:IDataAccessLayer
           {
                         public void Insert()
           }
                                     //Create Sql Insert query and fire
          }
}
Step 3 – Modify CustomerMiddleLayer as follows
Class CustomerMiddleLayer
{
              public void AddCustomer()
              {
                           ICustomerLayer layer= GetCustomerDataAccessLayer();
                           layer.Insert();
              }
              publicICustomerLayerGetCustomerDataAccessLayer()
             {
                           //Use DI or Factory pattern and return required Data Access Layer
             }
}

When CustomerXMLSqlLayer get introduced we will simply implement inherit from IDataAccessLayer and just change DI value in config value or Factory class.

You can also watch the below C# interface interview question with answer video from youtube.com 


References for c# interview preparation videos

Advertisements

About c# and .NET Interview questions

This blog is for developers who want to crack .NET and C# interviews. It has all tips and tricks needed to crack .NET interviews , C# interview , SQL Server interview , Java interview , WCF Interview , Silverlight interview , WPF interview , LINQ interview , Entity framework Interview. Do not forget to watch our Learn step by step video series. Learn MVC in 16 hours:- https://www.youtube.com/watch?v=Lp7nSImO5vk Learn AngularJS Step by Step:- https://www.youtube.com/watch?v=0kmdjqgO9IY Learn Design Pattern in 8 hours:- https://www.youtube.com/watch?v=YDobmucohqk Learn C# and .NET in 60 days:- https://www.youtube.com/watch?v=yh2SrzCkNQA Learn MSBI in 32 hours:- https://www.youtube.com/watch?v=mGPJx3ocFgg Learn SharePoint Step by Step in 8 hours:- https://youtu.be/C2fW76SwJNU
This entry was posted in Uncategorized and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s