8

In C# regarding the inheritance of constructors:

  1. I have read that constructors cannot be inherited.
  2. If the base class contains a constructor, one or more, the derived class have to always call one of them? It is not possible that the derived class can skip the constructor of the base class?
  3. Derived class can have its own constructor but it has to call one of the base class constructors.

Are these statements corrects?

3 Answers 3

14
  1. Yes, you're right, constructors are not inherited. So just because Object has a parameterless constructor, that doesn't mean that String has a parameterless constructor, for example. From section 1.6.7.1 of the C# 4 spec:

    Unlike other members, instance constructors are not inherited, and a class has no instance constructors other than those actually declared in the class. If no instance constructor is supplied for a class, then an empty one with no parameters is automatically provided.

  2. Yes, the constructor chain of a derived class constructor will always go through its base class constructor... although it may be indirectly, as it could chain to another constructor in the same class via this(...) instead of base(...). If you don't specify either this(...) or base(...), that's equivalent to base() - calling the parameterless constructor in the base class.

See my article on constructor chaining for more information.

Sign up to request clarification or add additional context in comments.

8 Comments

@Jon Skeet: Thank you, though I am not sure if I get it. If I declare a constructor in base class, according to the spec it will not be used by derived class.
@JeffreyZ: It will be used by the derived class if a derived class constructor calls it, either implicitly (if it's a parameterless base class constructor and the derived class constructor doesn't specify this(...) or base(...)) or explicitly (through a direct call to base(...)). If you could edit your question with a complete example which is confusing you, that would help. (Your third question is currently somewhat odd, too... you may want to edit it at the same time.)
@Jon Skeet: Oh right,also I need to call it explicitly or implicitly, but the base class constructor will alwayss be used, correct?
@JeffreyZ: A base class constructor will always be used, yes.
@fartwhif: If it generates partial classes, you can add the constructor in a separate file rather than editing the generated one. But no, there's no way of saying "derived classes should have a constructor with the same signature"
|
1

1. You cannot inherit constructors, but you can chain constructors. Example:

public class DerivedClass : BaseClass {
  public DerivedClass(object arg):base(arg){}
}

2. Yes, for example, you cannot instantiate a class derived from an object with all private constructors (except in the particular scenario that Jon mentions below in his comment). You cannot skip the constructor of a base class, but you can have multiple overloads, and select the constructor you choose to use.

3. Yes, not sure what the question is here.

9 Comments

Point 1 is definitely true. Constructors are not inherited.
In #1 you are chaining constructors, not inheriting them.
Maybe I misunderstood what was meant by inheriting the constructor. How would you describe what I wrote in my answer, if its not inheriting? ahh chaining...okay thanks
Calling? You can't construct a DerivedClass, without giving it a constructor, i.e. you can't inherit the base constructor and use that instead.
@tia: Parameterless constructors are not inherited. The compiler generates a parameterless constructor (calling a base class parameterless constructor) if you don't specify any constructors.
|
0

1 Yes

2 You cannot skip constructor but I think you could do nothing in default constructor. Just like

public class PClass {

    protected PClass() {
        // do nothing
    }

    public PClass(int myIntArg) {
        // initialize the class
    }

}

public Class CClass : PClass {

    public CClass(string myStringArg) : base() {
        // initialize the class
    }

}

3 Yes. See my previous example.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.