Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using base and this in constructor chaining

Tags:

c#

constructor

Say there is a base class called Employee that looks like the following

public Employee(string name, int id, float pay)
    : this(name, 0, id, pay, "") { }
public Employee(string name, int age, int id, float pay, string ssn)
{
    // Better!  Use properties when setting class data.
    // This reduces the amount of duplicate error checks.
    Name = name;
    Age = age;
    ID = id;
    Pay = pay;
    SocialSecurityNumber = ssn;
}

And a class Manager which inherits from Employee with a constructor like

public Manager(string fullName, int age, int empID,
  float currPay, string ssn, int numbOfOpts)
    : base(fullName, age, empID, currPay, ssn)
{
    .
    StockOptions = numbOfOpts;
}

To the best my understanding, the this keyword is just like the base keyword only it applies to constructors in the same class. My biggest question is, while reading a reference book, it says if you don't use the chaining the construction of a Manager object will include seven 'hits'. Since Manager inherits from Employees, does this mean that every Manager object is 'born' as an Employee and then becomes a Manager later? And after it is a manager you only have two fields to add instead of seven?

like image 686
wootscootinboogie Avatar asked Oct 12 '12 14:10

wootscootinboogie


People also ask

What is base in constructor?

The base keyword is used to access members of the base class from within a derived class: Call a method on the base class that has been overridden by another method. Specify which base-class constructor should be called when creating instances of the derived class.

Can you call multiple constructors within a constructor?

You can create multiple constructors in the same class, each with a different number of arguments that it accepts. To call one of the constructors in another constructor (of the same class), use the keyword this().

In what order should constructors be chained in a class hierarchy?

Because a superclass constructor is always invoked as the first statement of its subclass constructor, the body of the Object constructor always runs first, followed by the body of its subclass, and on down the class hierarchy to the class that is being instantiated.

How can constructor chaining be done by using the super keyword?

Constructor Chaining with super() keyword. If we want to call the constructor from the parent class, then we use the super keyword. Thus when there is more than one class with the relationship of inheritance, we need to use the super keyword in the child class to call the constructor of the parent class.


2 Answers

Yes, that is it.

The contructor parameters flow from the bottom up, then the object is created from the top down. It has to be like this in case your derived class need to access base class members in its constructor.

like image 155
Justin Harvey Avatar answered Oct 14 '22 11:10

Justin Harvey


In .NET, the complete object is created before any constructors get called. Next, the base constructor is called, then the derived class constructor.

So the answer is no - the manager object is not born as an employee and becomes a manager later. Rather, the manager object is born as a manager object ... before constructors get called.

BTW, in non-managed C++, it's the opposite, although enough memory is allocated for the derived class, the Employee object is created first (calling the Employee constructor) and any virtual methods called will result in despatching to the base class method bodies. Then the Manager class gets constructed.

like image 4
Dughall Avatar answered Oct 14 '22 12:10

Dughall