public class A2
{
protected virtual void f() {Console.WriteLine("from A2");}
protected virtual void g() {Console.WriteLine("From A2");}
protected virtual void h1() {Console.WriteLine("from A2");}
}
public class B2
{
protected virtual void f() {Console.WriteLine("B2");}
protected virtual void g() {Console.WriteLine("B2");}
protected virtual void h2() {Console.WriteLine("B2");}
}
giving this code above, I am trying to inherit all the functionality of the 2 classes by creating a new class as following:
public class C2
{
private A2 a2 = new A2();
private B2 b2 = new B2();
public void f()
{
a2.f();
}
public void h1()
{
a2.h1();
}
public void g()
{
b2.g();
}
public void h2()
{
b2.h2();
}
static public void Main()
{
C2 n = new C2();
n.f();
n.g();
n.h1();
n.h2();
}
}
However, when I'm trying to compile the program I'm getting an error that is I can't access the protected methods.
How would I inherit all the functionality without modifying the class A2, and B2?
protected, they cannot be accessed from outside of the class or a class which inherits from it.A2and then providesnewmethods that pass through to the base implementation, exposing the protected methods as public ones.protectedfields unless you're derived from the class. You'd have to make them public or write a wrapper that exposes them.