Click here to hide categories Click here to show left categories

User: Home          welcome : Guest          Log In / Register here     




.NET interview questions: - What is the best practice to update interface with new methods?

Practical scenario based .NET interview questions asked by interviewer on OOPS interface. So following is the practical explanation to it.

One liner answer: -

During interview one single simple sentence to answer it.

Interface once fixed should not be changed. If we ever have to add new functions, new interfaces should be created so that we do not break compatibility with old clients.

Explanation: -

The biggest use of interface is to ensure that strict CONTRACT is followed between clients and components. So that when changes happen on the components clients do not have to change too much.  In real world CONTRACTS between two parties do not change which implies that interfaces should also not change.

So if you want to add new methods or change methods in interfaces the best practice is to create new interfaces by inheriting. With this approach your older client who are using the interface stay happy and the new clients who want those new or changed methods get the benefits of the new functionality.

Let’s consider you have a simple interface called as “IView” which helps you to view data , below is the code for the same. Let’s consider that this interface is consumed by many clients.

interface Iview

{

        public void View();

}

Over a period of time some users demand “Edit” functionality as well but the rest of the users are happy with the ‘View” functionality and they do not want them to get affected by these changes. Now if you go and change this interface ( “IView”) a.k.a you will be disturbing everyone.

So the best option would be to add a new interface i.e. “IEdit” which inherits from “IView”. So the “IEdit”  will have the functionality for “Edit” as well as “View” because it’s also inheriting from “IView”.  And the clients who are consuming “IView” do not need to update as “IView’ is not changed at all.

interface IEdit : Iview

    {

        public void Edit();

    }

Also see following .NET interview questions video on implement interfaces with same method names in C#: -

Helpful Website Url
http://www.questpond.com/
Share this article   |    Print    |    Article read by 10410 times
Author:
Shivprasad koirala Koirala
I am a Microsoft MVP for ASP/ASP.NET and currently a CEO of a small E-learning company in India. We are very much active in making training videos , writing books and corporate trainings. Do visit my site http://www.questpond.com for .NET, C# , design pattern , WCF , Silverlight , LINQ , ASP.NET , ADO.NET , Sharepoint , UML , SQL Server training and Interview questions and answers
Related Articles:
Related Interview Questions: