0
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?

4
  • 2
    This isn't inheritance, this is having A2 and B2 as dependencies. Very different. As the members are marked protected, they cannot be accessed from outside of the class or a class which inherits from it. Commented Feb 24, 2020 at 19:28
  • 1
    You could make a wrapper class that inherits from A2 and then provides new methods that pass through to the base implementation, exposing the protected methods as public ones. Commented Feb 24, 2020 at 19:28
  • 1
    C# doesn't support multiple inheritance, and you can't access protected fields unless you're derived from the class. You'd have to make them public or write a wrapper that exposes them. Commented Feb 24, 2020 at 19:28
  • @RufusL You should mention that we use interfaces to achieve multiple class inheritance in C# Commented Feb 24, 2020 at 19:33

2 Answers 2

0

Derive classes that expose the behavior in public methods. Compose the composite class from the derived types.

public class A2_Derived : A2
{
    public void FMethod() => f();
    public void GMethod() => g();
    public void H1Method() => h1();
}

public class B2_Derived : B2
{
    public void FMethod() => f();
    public void GMethod() => g();
    public void H2Method() => h2();
}

public class C2 
{
    private A2_Derived a2 = new A2_Derived();
    private B2_Derived b2 = new B2_Derived();

    // implement the methods you need...
}
Sign up to request clarification or add additional context in comments.

3 Comments

It's good to NOT use obscure modifier like 'new', because they behave completely stupid with casting. In production development it is doomed at review level. Use interfaces.
This example doesn't have any methods where new would hide an inherited member anyway. The base class methods are named f, g, and h1 or h2. The derived class is using F, G, and H1 or H2. Or they were before the last edit :].
Yes, the case was intended to communicate that we were not trying to preserve method signature. Renamed the methods to communicate that more explicitly.
0

This is one of disadvantages of C# abstract and virtual classes - no multiple inheritance. Abstraction is essentially a dependance to parent, but why there should be one dependence?

As for the solution of your problem, you need to make them public before combining them:

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");}
}

//make a wrapper which will publish protected methods
//ADVICE: always use interfaces, they MUCH more flexible as you will see in your own example, use abstract/static modifier with caution.
public interface IA2
{
    void f_public();
    void g_public();
    void h1_public();
}
public class PublicA2 : A2, IA2
{  
   public void f_public(){f();}
   public void g_public(){g();}
   public void h1_public(){h1();}
}
//do the same for B2

And then you can combine them and even mark them with coresponding interfaces:

public class C2 : IA2, IB2
{
    private readonly IA2 _a2;
    private readonly IB2 _b2;

    public C2(IA2 a2, IB2 b2)
    {
        _a2 = a2;
        _b2 = b2;
    }

    public void f_public()
    {
        _a2.f_public();
    }

    public void h1_public()
    {
        _a2.h1_public();
    }

    public void g_public()
    {
        _b2.g_public();
    } 

    public void h2_public()
    {
        _b2.h2_public();
    } 
}

2 Comments

"This is one of disadvantages of abstract and virtual classes - no multiple inheritance" - hmmm... not exactly sure where do you see link between these two concepts... C++ has abstract and virtual classes... yet it has multiple inheritance just fine. You probably meant to say something different...
Well, in C++ it is much better If I can say so. In C# this is banned for whatever crazy reason Microsoft has. I said this in context of C#, many other languages has classes and I didn't mean them all, because for example Python will allow multiple inheritance.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.