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.