If I extend a c# interface, can I eat a call?

Question

In this question I mention Unity3D, but applies generally in c#.

Unity3D has an Interface that looks like this ..

public class SomeRobot:MonoBehaviour, IPointerDownHandler
    {
    public void OnPointerDown(PointerEventData data)
        {
        Debug.Log("Gets called whenever someone touches the screen...");
        }

No problem so far.

I make my own interface..

public interface IFUHandler:IPointerDownHandler
    {
    void OnBlah (PointerEventData data);
    }

Assume I have a daemon or whatever that calls OnBlah for consumers of the interface.

public class SomeRobot:MonoBehaviour, IFUHandler
    {
    public void OnBlah(PointerEventData data)
        {
        Debug.Log("Gets called when blah happens...");
        }

No problem so far. BUT

public class SomeRobot:MonoBehaviour, IFUHandler
    {
    public void OnBlah(PointerEventData data)
        {
        Debug.Log("Gets called when blah happens...");
        }
    public void OnPointerDown(PointerEventData data)
        {
        Debug.Log("this DOES still get called also...");
        }

In fact, OnPointerDown still gets called. Which makes sense.

BUT .... is there a way for my Interface to eat the calls to OnPointerDown?

So that a consumer subscribing to IFUHandler in fact does NOT get the calls to OnPointerDown?

(But of course gets my OnBlah calls.)

Or am I just suffering "TMIOOK" thinking? (Tied Myself In OO Knots thinking :) )


Show source
| c#   | interface   | unity3d   2016-11-13 17:11 1 Answers

Answers ( 1 )

  1. 2016-11-13 18:11

    Simple answer: No.

    Possible solutions:

    1. Don't inherit IPointerDownHandler when you don't need it. Why do you need this interface if you aren't going to use it?

    2. Create a Sealed Abstract class with a non-virtual method

      public class BaseFUHandler : MonoBehaviour, IFUHandler
      {
      
      public abstract void OnBlah(PointerEventData data);
      
      public void OnPointerDown(PointerEventData data)
      {
          //Gets called, but nothing hapends.
      }
      
◀ Go back