I have a base class that is marked as abstract. Is it possible to make a method in that base class only visible to other classes that are inheriting the base class?
Say I have Class1 that is my base class and is marked as abstract. Class2 Inherits Class1 and can make calls to all of it's public methods. I want Class3 to create an instance of Class2 but not be able to make calls to certain methods of Class1. I tried marking these methods as abstract themselves but then I get an error when Class2 tries to use them. The error is: "...Cannot declare a body because it is marked as abstract"
Why not declare the method protected
?
public abstract class Class1
{
protected abstract void Method1();
public abstract void Method2();
}
public class Class2 : Class1
{
protected override void Method1()
{
//Class3 cannot call this.
}
public override void Method2()
{
//class 3 can call this.
}
}
public class Class3
{
public void Method()
{
Class2 c2 = new Class2();
c2.Method1(); //Won't work
c2.Method2(); //will work
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With