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?
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.
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().
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.
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.
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.
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.
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