How not to implement a function of an interface in

2019-03-30 09:16发布

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

标签: c# oop interface
6条回答
走好不送
2楼-- · 2019-03-30 09:50

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.

查看更多
The star\"
3楼-- · 2019-03-30 09:53

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.

查看更多
爷、活的狠高调
4楼-- · 2019-03-30 09:53

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.

查看更多
看我几分像从前
5楼-- · 2019-03-30 09:54

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.

查看更多
虎瘦雄心在
6楼-- · 2019-03-30 09:54

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.

查看更多
【Aperson】
7楼-- · 2019-03-30 09:56

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();
}
查看更多
登录 后发表回答