Okay, so let's take it a step further where we are going to use constructor inheritance with multiple
derived classes.
So in our last video we had this example of the employee which is a person.
Now an employee could have multiple different positions, and each of those positions could be categorized
into a category like for example manager or developer or HR or whatever.
Okay.
So I'm going to create a new class here, public class manager which is also going to be an employee.
So this is another layer to our inheritance.
So it's multi-level so to speak.
And we obviously need to add the constructor.
So I'm going to generate the constructor here you can see it generated the constructor for me with all
of the base information we needed to give it.
So we need to give it the name, the age, the job title and the employee.
And now we can add more properties to our manager.
For example, how many people that manager is managing.
So that would be the team size for example, which could be our property that is specific to the manager.
Now you can also add more details to the manager at this point where we are also passing the team size.
So you see the ID already did the magic.
So here you see we have the employee ID and after that we also pass the team size to it.
And in the constructor.
So inside of the curly brackets here we are assigning whatever team size we gave the manager to the
team size property of the manager so that it's properly displayed and stored.
And now on top of that I'm going to use another method that will be display manager info.
And what Display Manager info will do is it will display the employee info.
Plus it will tell us about the team size.
And by the way the employee info.
Yeah that's fine here.
So I'm going to say team size colon.
And then in curly brackets I'm going to add the team size property okay.
So now when we are creating an object of our manager we are going to see how all of it comes together.
So we have Joe.
And now we are going to add a manager called Carl.
And Carl is going to be a new manager.
And now we need to pass in all this information so we could pass in the name and so forth.
Let's see what happens when we pass in Joe and the team size.
Unfortunately, that doesn't work.
I was hoping that something has changed with C-sharp 12, where it would just accept all of the properties
of the Joe that fits to the manager, whatever.
So we need to pass in a name.
So I'm going to call them Carl.
I'm going to give him a name, an age of let's say 45.
He's going to be a manager and his employee ID will be something else.
And his team size is going to be, let's say, seven, which they say is the perfect team size for a
manager to manage.
So there we are.
We have our manager Carl.
And let's use Carl to display not employee info but manager info okay so everything there is.
So let's get rid of this Joe statement here.
Because we just want to have Carl and see what's going on with this multi-level constructor here.
Multiple derived classes.
So you see the person constructor is called.
Then the employees constructor is called.
Then we show details about the name.
Well the person.
Then we show details about the employee and then we show the team size.
So the details about the manager.
So you see that these are three layers that we're now calling.
And we are using pretty much most of the things that we've learned about inheritance so far.
So we're calling the parent method inside of the child's method.
And we do the same thing here.
So we're calling the parent method not the grand parent method, but the parent method which internally
calls the grandparent method for our display manager info.
So now you could obviously extend it to whatever you want, like significantly more properties or whatever.
But the cool thing is that we see that what we just did works on multiple levels and with multiple derived
classes.
So this just shows you that you can make it as complex as you want.
And why it matters that there is a very clear and clean way how the base class is built, because whoever
builds the base class is building the fundament of our other code.
So now you might understand why it's so important that once a base class is built, you should not just
edit it.
If there are a bunch of deriving classes, because then you also have to edit the deriving classes,
potentially because if I now say, okay, we don't only have an age and the name for a person, but
we also have the hair color.
So let's say public string or address, I like that the ID just gives me address.
So let's just use that.
So then I would want to have the address as well.
So you see here it added the address to the parameters in the constructor as well as assigned it in
here.
But now when I do that you see that I need to change my employee.
And then I need to.
Well the manager is not complaining yet, but once I change it here, then I'll also potentially need
to change the manager.
So you see that this causes all kinds of errors.
And in our case it's easy because we are the ones controlling everything.
We're controlling the person class, we're controlling the employee class.
We're controlling the manager class.
But what if we are only controlling the employee class?
And now we weren't aware that the person class has been updated and now our code suddenly doesn't work
anymore.
Which is why sometimes when updates happen, suddenly, plugins that you have been using don't work
anymore because they were expecting for a certain class to be a certain way, or for a certain piece
of code to work a certain way.
But with the update that was changed and suddenly the API has been changed.
So basically, the access points with which you can get data from a website, for example, has changed
and suddenly you don't get the data anymore and your app doesn't display anything and it's useless.
Okay.
And you now need to look at what has changed.
And maybe the documentation hasn't been updated accordingly yet.
And you don't know what you need to change yet.
So you need to debug quite a bit or something, or dive deep into the code yourself.
Okay, so this was just to demonstrate why it could be a pain to have to update everything.
Okay, so I think this should have given you a good enough idea and some things to remember.
So constructor chaining is that constructors in derived classes should call the appropriate base class
constructor to ensure proper initialization of the base class.
Then we have the order of execution.
So the base class constructor is executed before the derived class constructor.
This ensures that the base class is fully initialized before any derived class specific initialization
occurs.
Then we have custom initialization, so the derived class can add their own initialization logic and
parameters, extending the functionality provided by the base class while reusing common initialization
code.
And then we have flexibility and reuse.
So constructor inheritance promotes flexibility in code reuse, allowing you to build complex object
hierarchies while maintaining clean and maintainable code.
So I'd say that's it for this video.
See you in the next one.