If I have a class like this:
public class Foo
{
public IEnumerable<Bar> Bars { get; set; }
public Foo()
{
Bars = new List<Bar>();
}
}
At some stage I re-factor the class and add a secondary constructor which implements the first one like this:
public class Foo
{
public IEnumerable<Bar> Bars { get; set; }
// some more properties were added
public Foo()
{
Bars = new List<Bar>();
}
public Foo(string parameter): this()
{
.... some code here
}
}
I could have also written it similar to this:
public class Foo
{
public IEnumerable<Bar> Bars { get; set; }
// some more properties were added too
public Foo()
{
InitilizeFoo();
}
public Foo(string parameter)
{
InitilizeFoo();
.... some code here
}
private void InitializeFoo()
{
Bars = new List<Bar>();
}
}
Seeing both approaches work in this scenario, is there a benefit or drawback in using one over the other?
Is inheriting constrcutors more efficient and making that code execute faster or is there a drawback which I don't know about making the second implementation more efficient instead?
To put it simply, you use multiple constructors for convenience (1st example) or to allow completely different initialization methods or different source types (2nd example. Show activity on this post. A class can have multiple constructors, as long as their signature (the parameters they take) are not the same.
The invocation of one constructor from another constructor within the same class or different class is known as constructor chaining in Java. If we have to call a constructor within the same class, we use 'this' keyword and if we want to call it from another class we use the 'super' keyword.
One of the benefits of using a constructor over a method is that you can be assured the constructor was called and the work within the constructor was performed. The language specifies that to construct an object a constructor must be called.
Providing Multiple Constructors With @classmethod in Python. A powerful technique for providing multiple constructors in Python is to use @classmethod . This decorator allows you to turn a regular method into a class method. Unlike regular methods, class methods don't take the current instance, self , as an argument.
One of the key benefits in having one constructor call another constructor is that you can set read-only fields that way, you can't do that by calling a non-constructor method.
For example:
public class Foo
{
private readonly int myNumber;
public Foo() : this(42)
{
}
public Foo(int num)
{
myNumber = num;
}
}
Performance wise, it's probably no more or less efficient to call another constructor than to call another method, but it is more readable, in my opinion, for a constructor to call another constructor than to call a separate, private method whose only point is to be called by a constructor.
There could, of course, be situations when having a separate method makes sense, and it's certainly not "wrong" per se. Chaining constructors just reads better to many for most uses, and there is no negative performance impact.
UPDATE: I performed 10,000,000 iterations of each way (chained vs private initialization method) and the results were so close they were nearly indistinguishable:
Initializer Method took: 84 ms for 10,000,000 iterations, 8.4E-06 ms/each.
Chained Constructors took: 81 ms for 10,000,000 iterations, 8.1E-06 ms/each.
So really, performance-wise there is nearly no benefit either way. The main benefit is with chained constructors you can set readonly
fields, and in most cases it is more readable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With