An interviewer a开发者_如何学运维sked 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
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 theReadOnlyCollection<T>
implementation ofICollection<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();
}
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.
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.
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.
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.
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.
精彩评论