EleventyOne
EleventyOne

Reputation: 7512

Inadvertent base class constructor "hiding" in derived class

[NOTE: Code written only to show problem, not because it's inherently useful.]

Let's say I have two classes, A and B:

public class A {

    public const int max = 2;
    public int[] id;

    public A() {
        this.id = new int[A.max];
    }

    public A(int id) : this() {
        this.id[0] = id;
    }

    public A(int id, int id2) : this() {
        this.id[0] = id;
        this.id[1] = id2;
    }
}

public class B : A {

    public B(int[] ids) : base() {
        int x = 0;
        while ( ( x < ids.Length ) && ( x < A.max ) ) {
            this.id[x] = ids[x];
            x++;
        }
    }
}

It seems that by creating the new constructor in B, I have inadvertently hidden all of the constructors that would normally be "inherited" by B from A (Indeed, I've learned that constructors aren't inherited in C#, but I'm still confused here).

For example:

A first = new A();  // fine

int[] x = new int[A.max-1];
B second = new B(x); // fine

B third = new B(12); // nope

If I remove the new constructor from B, then B third = new B(12); works just fine (in my terminology, the constructor isn't "hidden" anymore). But with the new constructor in B, that A(int) constructor isn't available to me anymore.

So is the solution REALLY to simply redefine ALL the constructors I want B to inherit from A and make them empty? Example:

public class B : A {

    public B(int id) : base(id) {} // adding this!

    public B(int[] ids) : base() {
        int x = 0;
        while ( ( x < ids.Length ) && ( x < A.max ) ) {
            this.id[x] = ids[x];
        }
    }
}

If I make that change, now B third = new B(12); works just fine. But it seems a little ugly that I have to redefine every constructor I want to use in the base class of a derived class, simply because I'm adding a new constructor to that derived class!

Upvotes: 1

Views: 706

Answers (2)

Macilquham
Macilquham

Reputation: 282

I think you are absolutely correct EleventyOne your example is clunky, but the problem isn't the language it is your design. I know your example isn't a real world example and as such abstract questions will produce abstract answers.

If you find yourself with many constructors on your base class chances your design is wrong. Potentially you are trying to model something in inheritance that is better suited for composition or your base class should be taking in an object instead of individual related properties, if the properties aren't related then perhaps your base class is doing too much (Single Responsibility Principle).

When it doesn't feel right chances are you have a code smell! :)

Upvotes: 0

Alexei Levenkov
Alexei Levenkov

Reputation: 100547

Yes, derived class does not get nor exposes constructors from base class. You need to define all necessary constructors in base class (possibly calling base class' constructor).

Note: if you derived class has nothing to initialize you may want to reconsider if you really need new class.

Upvotes: 3

Related Questions