Welcome back.
In this video we're going to look at constructor inheritance in C sharp.
So let's quickly recap what constructors are.
They are special methods in a class that are called when an instance of the class is created.
So how does that matter in the context of inheritance?
Well, constructors of the base class are called before the constructor of the derived class is called.
This ensures that the base class is properly initialized before any additional initialization in the
derived class takes place.
So why does constructor inheritance matter?
Why is it important?
First of all, proper initialization, which ensures that all fields and properties of the base class
are correctly set up before any operations of the derived class can take place.
Otherwise, this could add a bunch of challenges and errors along the way, where you are trying to
access something that isn't initialized yet and the second aspect why this matters is consistent state.
So this maintains a consistent state and a valid state across the object hierarchy.
Then there is a third aspect, and that is the reuse of initialization code, which avoids duplication
of initialization code by reusing the base class constructor.
So let's look at an example here.
Therefore I have this class person here with two properties.
So the name and the age where the setter is private because we don't need to set it somewhere else.
Then we have this base class constructor.
So a person can have a name and an age.
So we're setting those and we're just saying the constructor of the person is called.
And then we have this public void which allows us to display some info about that person.
So the name and the age.
Now we have this other deriving class which is called employee.
So it inherits the person's traits, which means the person's members which are in this case all of
the properties as well as the constructor and its method.
Okay, so what we add for the employee are two more properties.
So now instead of just having a name and an age, we also have a job title and an employee ID.
Okay, so now in our derived class what we need to do is we need to do a few things.
First of all, we need to assign the name the age.
Then we also need to assign the job title and the employee ID.
Now what we are passing on to the base is the name and the age.
And by doing this here.
So this colon base we are calling the base class constructor.
If we look at it, if you recall, the base class constructor is the person constructor.
So the constructor of the person class, it needed a name and an age.
Where do I get this name and age from?
I get it from here.
So whenever I'm creating an employee, I need to pass in the name and the age, okay?
And I'm passing those further to the base constructor, which then sets those up and does all of the
magic that is required there.
And then in the next step, I'm setting up the job title as well as employee ID.
So you see here I didn't need to add these lines here where I assigned the name and the age, because
that happens in the base class.
That's because we're calling the base class's constructor, which then assigns the properties accordingly.
So our employee will still have all four of them, will not just have the job title and the employee
ID, but it will also have the name and age.
And then we're saying the employee constructor is called.
Now the next step would be to display information about the employee.
So what we do here is we call the base class method.
You see we can just call display person info without having to create an object of our people or person
class.
We can just call its methods directly as if they were in the same class.
And then with console dot write line with the job title and the employee ID on top.
So now let's go ahead and create an employee.
So employee I'm going to call him Joe.
And he's a new employee.
And here I need to pass in a bunch of details.
So Joe is his name.
His age is 36 I believe, or 37, maybe even.
He is a sales rep and he has the employee ID of one two, three, four.
And now I'm going to call.
Joe Dot display employee info, which internally will also call display person info.
So now if we look at it, we will see that the person constructor is called the employee constructor
is called the name and age of Joe is displayed, and the job title is displayed as well as the employee
ID, so all of the info.
So this is coming from the person class.
This is coming from the employee class.
This is coming from the person constructor.
This is coming from the employee constructor okay.
So here you see how all of this is coming together.
Everything is being combined here okay.
So I'd say that's it for this video we're going to dive deeper just a tiny bit in one of the next videos.
But first let's try to understand when and why inheritance is useful okay.
So see you in the next video.