So we work with our department quite a bit here and I created my accounting department.
Now for the application we're building we might need departments of course but we might need more information
for the specific types of departments we might have an I.T. department which has an I.D. which has a
name which has employees but which also has administrators something which only the I.T. Department
has for example we might have the accounting department which might all have an I.D. or a name and employees
but it might also have let's say reports in Israel for reports it degenerated and so on.
So we might have some base properties and also methods which all departments should have.
But then we might have specialized workings of these apartments with their own specific properties and
methods which only that the part that might have an inheritance can help us implement something like
this.
So how can we utilize inheritance here.
Well we have our class department now let's create these specialized departments.
So here we could have a class I.T. department and in this class here I want to utilize the name and
the I.D. and the employees of the general department.
But I want to add my own features.
Now we can inherit the department class by using the extensions keyword here.
And then the class we want to inherit.
In this case that's department.
Now important you can only inherited from one class so you can't inherit from multiple classes.
If you would need that.
So now you're inheriting from department.
And one consequence is that if I now create an I.T. department here I indeed can call it like this with
this kind of constructor.
Even though the I.T. Department class is empty if we save that we indeed see it works.
Ask before because when we inherit from another class the class which inherits automatically gets everything
the base class department in this case has including its constructor.
So as long as we don't add a dedicated constructor Ford is inherited class so Ford to subclass the base
classes constructor.
So this constructor is automatically used when we instantiate our subclass.
So we in the end magically call the base class constructor with the arguments per passenger.
Now we can add our own constructor though by adding constructor here in I.T. department.
And now you see I'm getting errors.
For one we now of course should accept the data we're getting here though we could argue the name should
not be something we need to pass in here and it shouldn't also be accounting but instead here the name
of the department since it's the I.T. Department class can always be I.T..
I still want to accept my I.D. though.
But now I want to essentially forward that just like you fixed name I.T. to the constructor of department
and for that we have a special keyword we we can and in this case we have to use and that's super whenever
you add your own constructor in a class that inherits from a number of class you have to add super Indian
heritage class and you have to execute it like a function super here calls the constructor of the base
class.
So department's constructor in this case and now they are first super takes the arguments of the parent
class constructor the I.D. and the name.
So here it can forward I.D..
So I.D. which I'm getting here is just passed to super.
And it can hard code a value for the name like a T.
So this will now call the constructor of the base class from inside the subclass not important.
You have to call super first in your constructor before you do anything with the this keyword.
So if you plan on assigning any other special properties here you have to do that after calling Super.
And indeed I want to add my own special properties here.
We could add our admins here let's say as a private whereas a public actually as a public property admins
and that should be an array of strings let's say.
Well then I don't want to forward this to my base class because the department class doesn't want any
administrator data.
Instead I want to store it is in a property of I.T. department.
So here we can then add admins as a field which takes a couple of administrators and store admins in
there or because I'm using public and here I'm already using that shortcut where a property of the same
name will be created and the data we feed into this argument is stored in that automatically created
property still to make it clear that super has to be called first.
I'll take the longer route add Adam Edmonds here like that and that here say this dot admins equals
admins.
Again the shortcut would be shorter.
I'm doing this here to show and make it really clear that if you are using something that uses the this
keyword you have to do that after calling Super.
And with that if we save that I'm getting an error because I'm not passing in any admin so let's do
that here.
Max is the only admin I'm passing in here.
Now we can save that and compiled it without errors.
And now here indeed we see our department here.
It now has the name I.T. and if we can so lock the entire department and we may be all the renamed is
from accounting to I.T. and then console lock I.T. here of course renaming that while it's optional
just makes more sense given that we're building an I.T. department here now that a console locked entire
department we indeed see we got admins but we also got employees and everything which I set up in my
department class here.
So that's inheritance and action.
A very useful feature because now of course we can all do create our accounting department here by extending
department and there we could then add our accounting specific things so we can still use a constructor
here.
And now maybe with the shortcut except a couple of reports here which might be a private property and
then we have ADD report which takes text which should be a string and then we reach out to reports here
and push text as item into the report and maybe we also have get reports here and there we just console
lock this reports or we name it print report stage 4.
Makes more sense and now we would have a very special Where's another department with two methods which
the pace department doesn't have.
And with extra property which to base department doesn't have and still we get everything from the base
department the name which I'll set to accounting here and the I.D. which we all look forward to the
base department constructor.
And so with that down there if we create our accounting here with new accounting department we can feed
in our I.D. and then our reports let's say that's an empty array initially then we can call at report.
Something went wrong never that good of course in the accounting I guess.
And then we can also print our reports here like that.
And with all of that it compiles just fine and we get the reports here and we see the I.T. Department
for example wouldn't have any reports.
The accounting department does because of inheritance where we inherit certain features and add our
own features.