How to implement only few methods 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();
 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 šŸ™‚ 

via Blogger January 21, 2017 at 05:38PM


Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s