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.
Use
sealed protected override bool Test()
in Sub1No, 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:That has to compile:
Sub2
is derived fromSub1
.s1.Test()
to compile, where the compile-time type ofs1
isSub1
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 inSub1
but still share the same properties withSub2
. This can be achieved by changing the inheritance chain from this:to this: