tags:

views:

295

answers:

6

An interviewer asked me the following question in interview, but I don't know what could be the answer of this question, please help!!!

What must be done if i don't want to implement a function in my class that is declared in an interface which is implemented by my class.

Edited: I am using .NET with C#. It will be great if anyone can provide a sample code example in C#.

Thanks

+12  A: 

Implement the function, but throw an exception in the implementation.

In .NET, you typically use one of the following (there are similar exception types in other languages).

  • NotImplementedException: There is not yet an implementation of the method.

  • NotSupportedException: There will not be an implementation of the method in this class, by design. This appears several times in virtual methods of a base class, where a derived class is expected to offer an implementation where applicable. It also appears in explicit interface implementation methods (← this is the specific thing the interviewer asked), such as the ReadOnlyCollection<T> implementation of ICollection<T>.Add, etc.

For example, in C#:

public void MyNotSupportedInterfaceMethod()
{
  throw new NotImplementedException();
}

UPDATE: as marcos points out, if your class is abstract, you can leave the implementation to the inheritor, but you still have to declare it:

public abstract class MyClass : ISomeInterface
{
   public abstract void MyNotImplementedInterfaceMethod();
}
Philippe Leybaert
But @Philippe, I think in that case we are implementing it in the class.
Prashant
There's no other way. C# doesn't have the concept of optional interface methods (like in Objective C for example)
Philippe Leybaert
Yes indeed we are, so the interviewer may have meant what if your implementation doesn't support that particular feature of application logic than literally not implementing the function.
Murali VP
OK, So overall you must have to implement the function but you can bypass it by throwing an exception.
Prashant
@Prashant Not if you make your class abstract.
marcos
I added detailed information about when you might choose to throw what. :)
280Z28
Thanks, but I think InvalidOperationException does not belong in this list (especially for this question). So if you don't mind, I'll remove that one.
Philippe Leybaert
The reason I added it is I've seen people (me in the past, for one) find `InvalidOperationException` and start using it instead of `NotSupportedException` because the distinction between those two wasn't clear.
280Z28
IMO InvalidOperationException is always "something is wrong" exception. Whereas NotSupported/Implemented are much more specific
Quibblesome
@Quibblesome: The specific cases where `InvalidOperationException` should be used are describe if you look at the edit history for this post. ;)
280Z28
+4  A: 

Don't know about c#, but in java you could make the class abstract. That way you can leave the implementation to the next non abstract class to extend your abstract class.

marcos
True, but you still have to declare the interface function. You don't have to implement it of course.
Philippe Leybaert
+7  A: 

These questions are always difficult to answer because it is hard to know what the interviewer was getting at. This question doesn't have a direct answer as all classes that implement an interface MUST implement its methods. You may be overthinking it if you are looking for some code to answer it.

In my opinion this question only has value if asked verbally as there is no real answer, but you can learn how the interviewee thinks. Possible answers could be:

  • Implement the method but throw an exception.
  • Make the class abstract and declare the method as abstract.
  • Remove the method from the interface definition.
  • I don't think this is possible.

All are valid, none really answer the question fully.

Jack Ryan
+1  A: 

To me, this is something of a trick question. If your class is implementing the interface, then it should implement every method. If there is a method on the interface that you don't want to implement, it means you don't want to implement that interface, you want to implement another interface that is similar.

Obviously there are cases that you have to implement an interface to allow your class to be used with another class or method. In those cases, you should be reviewing the class/method that has defined this contract. Change it if you can, if you can't then you implement a method specific to the situation (this will probably be either do nothing or throw an exception) but with the knowledge that you are not really fulfilling their contract and at any point their code could change causing your code to fail.

Chris Shaffer
+6  A: 

I suspect that the interviewer was trying to elicit the response "use an explicit interface implementation". If you say

class C : IFoo
{
    void IFoo.Foo() {}
}

then you can use Foo via the interface, but not via the class:

C c = new C();
c.Foo(); // illegal!
IFoo ifoo = c;
ifoo.Foo(); // legal

If that was what the interviewer was getting at then the interviewer needs to do two things.

First, they should have said "how do I implement an interface without exposing the methods of the interface on the class?"

Second, they shouldn't have asked the question in the first place. A poorly-phrased question where the interviewer is seeking a specific answer does not actually tell you much about the candidate! A better question on that topic would be "How do you decide whether to use explicit or implicit implementation when implementing methods of an interface?" That tells you about how the candidate makes technical decisions.

Eric Lippert
Or (as is often the case), the interviewer is no match for the candidate, and he/she wants to look smart by asking questions the interviewer thinks there's only one correct answer for.
Philippe Leybaert
A: 

Revise the interface if possible, since this is a violation of the Interface Segregation Principle.

EDIT: If it's not possible, go with Philippe Leybaert's excellent answer.

TrueWill