If at first you don't succeed, call it version 1.0

Contact Me

Sarvesh Kushwaha
Email : sarveshkushwaha@outlook.com

Total Pageviews

Powered by Blogger.

Saturday, 21 January 2017

How to implement only few signature of an Interface ? #AspNetInterviewQuestionsSeries


lets make this question situational :) .An interviewer can ask you like this :


Lets suppose i am having an Interface and this Interface consisting five methods signatures.This Interface is already being consumed by our thousand of customer and now new customer came and asked that we need only three method inside that. what will you do to resolve this situation or what will you suggest to your customer ? 

Possible solutions are to this situation are as follows :

  1. Implement that Interface using an Abstract class and mark those methods as abstract which you don't want to implement.
  2. If class should be concrete then you can implement rest of the functions and throw exceptions like  NotImplementedException or NotSupportedException .
  3. Last you can segregate that Interface further into two interface and implement those in class.But then in above scenario it will become a tough situation.

Below is the running example :



Code Text :


using System;
     
public class Program
{
 public static void Main()
 {
  ConcreteClass C = new ConcreteClass();
  
  C.DoSomethingElse3();
  
 }
 public interface Iinterface
 {
     void DoSomething();
  void DoSomethingElse();
  void DoSomethingElse1();
  //customer dont want to implemnet below ones
  void DoSomethingElse2(); 
  void DoSomethingElse3();
 }
 
 //Solution 1 : create an abstract class 
 public abstract class MyClass: Iinterface 
 {
  public void DoSomething(){}
     public void DoSomethingElse(){}
  public void DoSomethingElse1(){}
  
  // Mark those Abstract which you dont want to implement 
  public  abstract void DoSomethingElse2();
  public abstract void DoSomethingElse3();
     
 
 }
 
 //Solution 2 : Use these keywords NotImplementedException/NotSupportedException
 public class ConcreteClass : Iinterface
 {
  public void DoSomething(){}
     public void DoSomethingElse(){}
  public void DoSomethingElse1(){}
  
  // keywords NotImplementedException/NotSupportedException
  public  void DoSomethingElse2()
  {
   throw new NotSupportedException();
  }
  
  public  void DoSomethingElse3()
  {
      throw new NotImplementedException();
  }
  
 
 }
 // Solution 3 : Segregate interface into logical units this should how we design our application in the beginning,
 
 
}
Hope my this series will help somebody to get a job :) :) . Happy coding :) 

0 comments:

Post a Comment