43

Is it possible to override the constructor of the base class in the derived class?

If so, the how can it be accomplished and in what use case would this be practical? If not, why not?

durron597
  • 31,968
  • 17
  • 99
  • 158
naval
  • 1,423
  • 1
  • 13
  • 26

3 Answers3

58

No, you can't override constructors. The concept makes no sense in C#, because constructors simply aren't invoked polymorphically. You always state which class you're trying to construct, and the arguments to the constructor.

Constructors aren't inherited at all - but all constructors from a derived class must chain either to another constructor in the same class, or to one of the constructors in the base class. If you don't do this explicitly, the compiler implicitly chains to the parameterless constructor of the base class (and an error occurs if that constructor doesn't exist or is inaccessible).

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 5
    "The concept makes no sense, really - constructors simply aren't invoked polymorphically." - in Delphi, they are. It is a very useful feature and I find the workarounds required in C#, either by using reflection and passing around runtime-checked `Type` instances, or by supplying separate factory classes, rather cumbersome in comparison. – O. R. Mapper Aug 07 '15 at 13:28
  • 8
    @O.R.Mapper: Well yes, I was talking in the context of C#. What other languages do is irrelevant to this answer. – Jon Skeet Aug 07 '15 at 13:43
  • 5
    In that case, I suggest the answer doesn't claim "the concept makes no sense, really", as that sounds pretty general. Something like "The concept would make a lot of sense, but it is not possible to make use of the concept in C#, as constructors aren't invoked polymorphically there." might be clearer. – O. R. Mapper Aug 07 '15 at 13:46
  • 1
    @O.R.Mapper: I've adjusted it to make it clear that I'm talking about C# - I see no reason to talk about the concept "making a lot of sense" when it's not something I particularly believe. Without having experience of Delphi, I can't say how useful it is or isn't in that context, and I'm not just going to accept your word for it being a good thing. – Jon Skeet Aug 07 '15 at 13:49
  • 2
    Fair enough - as long as "I can't say how useful it is or isn't" doesn't lead to the conclusion "It generally doesn't make any sense.", I'm happy :) – O. R. Mapper Aug 07 '15 at 13:51
  • 3
    For the sake of completeness, [here](http://stackoverflow.com/a/24605022/1430156), [here](http://stackoverflow.com/questions/1584670/what-design-patterns-do-you-implement-in-common-delphi-programming/1585004#1585004), and [here](http://stackoverflow.com/questions/9280546/overriden-and-non-overridden-constructor/9280856#9280856) are some examples of the respective Delphi feature, just to provide a reference to future visitors for my claim that this feature exists. – O. R. Mapper Aug 07 '15 at 13:56
  • Hi, Jon. Im working with this [**example**](http://i.stack.imgur.com/B898F.png) here change the original constructor to include one aditional paramater. I wonder that doesnt count as override constructors? or have a different name? – Juan Carlos Oropeza Aug 30 '16 at 19:47
  • 1
    @JuanCarlosOropeza: No, that's not overriding. You're just declaring a constructor with three parameters, and chaining to a base class constructor with two parameters. – Jon Skeet Aug 30 '16 at 19:50
16

No, Constructors are not inherited. You can not override them in derived class.
Reason

A base constructor will always be called, for every class that descends from object, because every class must have at least one constructor that calls a base() constructor (explicitly or implicitly) and every call to a this() constructor must ultimately call a base() constructor.

Shezi
  • 1,352
  • 4
  • 27
  • 51
8

No, you can't override the constructor.

If you take a look at the basic syntax of a constructor, it should have the same name as the class you are writing it for.

Lets say,you write a method with the same name as that of the base class(same as that of base class constructor), its just gonna be a new method in the derived class without return type specified, which is syntactically incorrect.

Vizard
  • 303
  • 1
  • 8