Questions tagged [constructor-chaining]

Constructor chaining is the process of calling the super class constructor by the subclass constructors in the inheritance tree when creating an object.

Constructor chaining occurs through the use of inheritance. A subclass constructor method's first task is to call its superclass' constructor method. This ensures that the creation of the subclass object starts with the initialization of the classes above it in the inheritance chain.

There could be any number of classes in an inheritance chain. Every constructor method will call up the chain until the class at the top has been reached and initialized. Then each subsequent class below is initialized as the chain winds back down to the original subclass. This process is called constructor chaining.

76 questions
265
votes
9 answers

How to do constructor chaining in C#

I know that this is supposedly a super simple question, but I've been struggling with the concept for some time now. My question is, how do you chain constructors in C#? I'm in my first OOP class, so I'm just learning. I don't understand how…
Alex
  • 64,178
  • 48
  • 151
  • 180
157
votes
7 answers

C# constructor execution order

In C#, when you do Class(Type param1, Type param2) : base(param1) is the constructor of the class executed first, and then the superclass constructor is called or does it call the base constructor first?
webdreamer
  • 2,359
  • 3
  • 23
  • 30
45
votes
5 answers

Constructor chaining in C++

My understanding of constructor chaining is that , when there are more than one constructors in a class (overloaded constructors) , if one of them tries to call another constructor,then this process is called CONSTRUCTOR CHAINING , which is not…
progammer
  • 1,951
  • 11
  • 28
  • 50
44
votes
7 answers

Initialize field before super constructor runs?

In Java, is there any way to initialize a field before the super constructor runs? Even the ugliest hacks I can come up with are rejected by the compiler: class Base { Base(String someParameter) { System.out.println(this); …
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
41
votes
4 answers

Delphi: Understanding constructors

i'm looking to understand virtual override overload reintroduce when applied to object constructors. Every time i randomly add keywords until the compiler shuts up - and (after 12 years of developing with Delphi) i'd rather know what i'm doing,…
Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
39
votes
7 answers

What does this colon (:) mean?

Before the this keyword is a colon. Can anyone explain what the colon means in this context? I don't believe this is inhertance. Thanks using System; namespace LinkedListLibrary { class ListNode { private object data; …
Ken
23
votes
2 answers

Constructor chaining in PowerShell - call other constructors in the same class

I was doing some testing and stumbled upon the following: You can overload methods in PoShv5 as you wish. If you call the method without parameters, it can internally call the method with parameters, to keep your code non-redundant. I expected this…
17
votes
7 answers

: this(foo) syntax in C# constructors?

Every now and then, I bump into syntax that I've seen before, but never used. This is one of those times. Can someone explain the purpose of ":this" or ":base" following a C# constructor method? For example: public MyClass(SomeArg arg) : this(new…
FlySwat
  • 172,459
  • 74
  • 246
  • 311
12
votes
6 answers

Delphi: How to hide ancestor constructors?

Update: gutted the question with a simpler example, that isn't answered by the originally accepted answer Given the following class, and its ancestor: TComputer = class(TObject) public constructor Create(Teapot: string=''); end; TCellPhone =…
Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
10
votes
7 answers

: this() As a constructor

I'm trying to get a better understanding of general practice... specifically deriving this() in a constructor. I understand that its less code, but I consider it less readable. Is it common/good practice to do it this way? Or is it better to…
Aardvark
  • 1,264
  • 1
  • 12
  • 21
8
votes
4 answers

Delphi: How to add a different constructor to a descendant?

Update: The example i originally had was kind of complex. Here's a simple 8 line example that explains everything in one code block. The following does not compile gives a warning: TComputer = class(TObject) public constructor Create(Cup:…
Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
8
votes
1 answer

Are code contracts guaranteed to be evaluated before chained constructors are called?

Before I started using Code Contracts I sometimes ran into fiddlyness relating to parameter validation when using constructor chaining. This is easiest to explain with a (contrived) example: class Test { public Test(int i) { if (i ==…
Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
7
votes
2 answers

Constructor chaining in conjunction with the base constructor invocation

Say I have the following: class Base { public Base (int n) { } public Base (Object1 n, Object2 m) { } } class Derived : Base { string S; public Derived (string s, int n) : base(n) { S = s; } public Derived (string…
Andreas Grech
  • 105,982
  • 98
  • 297
  • 360
7
votes
3 answers

Understanding constructor visibility

Here's two simple classes, initially both have no keywords (virtual, overload, override, reintroduce): TComputer = class(TObject) public constructor Create(Teapot: Integer); end; TCellPhone = class(TComputer) public constructor Create(Teapot:…
Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
7
votes
4 answers

Delphi: When does reintroduce hide ancestors and when does it show them?

Today Recently on Stackoverflow i learned that: reintroduce is used to hide ancestor constructors reintroduce is used to show ancestor constructors i've been trying to make sense of it all, so here is a another, very specific question, supporting…
Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
1
2 3 4 5 6