Home   Cover Cover Cover Cover

Inheritance and constructors

Question: Why can a subclass inherit the fields, methods, properties and indexers from the superclass, but not the constructors?

Answer: A constructor must have the same name as the class to which it belongs. Therefore a base class constructor cannot be inherited by a subclass, because it has the name of the base class and not the name of the subclass.

A constructor is used to initialize fields of the class to which it belongs. A base class constructor initializes fields of the base class and not fields of the subclass. The subclass usually has additional fields, which are unknown to the base class constructor and thus cannot be initialized by it. Therefore constructors are never inherited but must be implemented anew for every class.