In this video we're going to look at how properties are inherited.
And we're going to start looking at the constructor inheritance.
But we're going to go deeper into it in the next few videos.
Okay.
So first of all, I have this public class person, right?
And I have a constructor which is the base class constructor, which is just going to say person constructor
called.
And then I have this empty method called Display person info.
Now the information that I want to store about the person are going to be the name.
So this will be a string name and I will set the private or the setter private.
And then I'm going to have another property which will be the age.
And again the setter will be private.
So nobody else can set the age except for my person class.
So now you see the person constructor well has these non-nullable properties and whenever setting them.
So let's set those.
Let's just say whenever we're creating a person, we need to pass in the name and we need to pass in
the age.
And in here we can then go ahead and say name is going to be name, and age is going to be age.
Okay.
So that's nothing new.
We've seen that multiple times before where we create a constructor and a few properties.
So here the properties this is the constructor.
And then we assign the values or initialize the values for the properties with the constructor.
Now how does that work for inherited classes.
So therefore I'm going to create another class down here which I'm going to call public class employee.
And this class will inherit from person because every employee is also a person.
Now you can already see that it says there is no argument given that corresponds to the required parameter
name of person string int.
All right.
So that means that we need to assign a name and an int for our person.
Now I could go ahead and create a int or like properties in here.
But the cool thing is I don't have to because the parent already has these properties.
It's going to inherit them over to me, so I don't have to worry about it.
I can just use them.
So how can I use them?
Well, let's go ahead and just create a employee constructor here.
And you can see the constructor isn't happy.
It says there is no argument given that corresponds to the required parameters name person string int.
So how can I now pass those details?
Well, the thing is, even when I'm creating an employee, I still need to pass in the name and I still
need to pass in the age.
Okay, now however, I need to pass them on to my base.
What is the base?
Well, the base is the constructor of the parent.
So this base here follows the same definition of the constructor of my person.
So you see here person had a string and an int.
Now I called them name and age because that's the variables that I'm going to pass on to my base.
So the base class will now get the name and the age that I am passing on to the employee, and then
it will do all of its magic.
Now the cool thing is I don't have to add anything here.
I will do that later on.
We're going to add more properties to the employee.
You're going to see that in the next video.
Okay.
But for now I just want to show you how this base keyword can also be used with the constructor.
So now whenever I'm creating an employee, let's say I am displaying I want to display person info.
And I'm going to keep it simple.
I'm just going to say something like Console.WriteLine.
And then I'm going to display the name and the age.
Okay.
So nothing too fancy now when I'm creating an employee.
So let's say I'm creating an employee and I call him Joe.
And Joe is going to be a new employee.
I need to pass a name and an age.
Okay.
So I'm going to pass in the name Joe, and I'm passing the age of let's say, 36.
Now, what I'm going to add here as well is I'm just going to display the person info.
So I want to know everything about the person Joe, even though it's an employee.
Okay.
Because Joe might be an employee, but Joe is also a person because employee is inheriting from person,
which means that it has all of the capabilities of person, which also means that it has all of the
properties.
Okay, so now let me just add a constructor statement here.
Inside of the employee constructor I'm going to say um, employee constructor called.
So here I'm maybe going to add.
Something like derived class, so it's a little easier to understand.
Okay, now let's run this and you will see that it says person constructor called.
And then it says employee constructor called.
And then it says name Joe age 36.
So how does all of that come together?
Well the thing is a person has these two properties and every class that inherits from person will also
have these properties.
Now what I'm doing in the constructor is I'm assigning values to those properties.
And I'm also just calling the right line statement here saying okay, the person constructor is called.
I do that so that you see the flow okay.
The flow is even though we are creating an employee, first the person constructor is called where some
initialization is happening.
So some properties are set.
In our case all properties are set.
And then I'm calling the employee constructor.
So the employee constructor currently doesn't do much.
We're going to add that in the next video.
I just really wanted to make sure that you understand this core concept first.
And then what I'm doing is I'm calling this method here display person info onto our Joe employee who
also is a person, which is why we can use this method.
And the method is public.
It's not private, it's public.
So we can use the method on any given employee.
So you see, even though our employee class currently is pretty empty and rather useless, I would say
we now can create employees and we could extend the employee class to our liking.
We could add properties like the employee ID, like salary, like position, all of these things we
can add to an employee because not every person has an employee ID that every person has.
An employee has a salary, that every person has a position.
Okay.
So this was basically the preparation for the next video, which will go a step further.
The point that I really wanted to bring home are that properties are passed on to deriving classes,
and that there is this base keyword that you need to use whenever you have a constructor that defines
or uses parameters, and you need to pass on these parameters even as an employee.
Now you can extend that.
And that's what we're going to do in the next video.
Now the cool thing is really this name is basically passed on to this here, so passed on to the base.
That's what what happens internally.
Same for H.
It is passed onto the base.
And we can also use it here by the way.
So we can also say name is equal to whatever Frank.
We could override that even though in this particular case it would be stupid because all people would
now be called frank, which is why I'm getting rid of it.
So I'd say let's dive deeper in it in the next video.