简体   繁体   中英

Prevent calling base class implemented interface method in the derived class C#

Is it possible to implement an interface in a base class and allow calling/overriding the implemented method in the first derived class level but prevent calling it from any further derived classes?

    public interface IInterfaceSample
    {
        bool Test();
    }

    public class Base: IInterfaceSample
    {
        public virtual bool Test()
        {
             return True;
        }
    }

    public class Sub1: Base
    {
        //I need to be able to override the Test method here
        public override bool Test()
        {
             return True;
        }
    }

    //Under a separate project:
    public class Sub2: Sub1
    {
       //I need to prevent overriding the interface implementation in this class
    }

Now what i need is this:

    var b = new Base();
    b.Test();//This should work

    var s1 = new Sub1();
    s1.Test();//I need this to work too

    var s2 = new Sub2();
    s2.Test();//I need to prevent doing this

So far from research i think this might not be possible because interfaces has to be public, otherwise there is no real value of using them.

In my case, i need class Sub2 to have access to the properties in Sub1 but only that and no access to the methods on that class and specially the interface implementation methods.

The only way i was able to do this was to not use the interfaces at all and do it like this:

    public class Base
    {
        internal virtual bool Test()
        {
             return True;
        }
    }

    public class Sub1: Base
    {
        //I am able to override the Test method here
        internal override bool Test()
        {
             return True;
        }
    }

    //Under a separate project:
    public class Sub2: Sub1
    {
       //Nothing to override here which is what i need
    }

    var b = new Base();
    b.Test();//This works

    var s1 = new Sub1();
    s1.Test();//This works too

    var s2 = new Sub2();
    s2.Test();//This is prevented

However i am wondering if this is still available to achieve with interfaces, any help is much appreciated.

No, this isn't possible - it would break the whole point of polymorphism. In particular, imagine you didn't use var , but used the types explicitly:

Sub1 s2 = new Sub2();
s2.Test();

That has to compile:

  • The first line has to compile because Sub2 is derived from Sub1 .
  • The second line has to compile because you wanted s1.Test() to compile, where the compile-time type of s1 is Sub1 as well.

As a rule of thumb, if you have two classes X and Y, and only some of the public operations on X are valid for Y, then Y shouldn't derive from X. You should be able to treat any instance of a derived class as if it's an instance of the base class (and all interfaces it implements).

You want the Test method to be available only in Sub1 but still share the same properties with Sub2 . This can be achieved by changing the inheritance chain from this:
在此处输入图片说明
to this:
在此处输入图片说明

在Sub1中使用sealed protected override bool Test()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM