Welcome back.
And this video I would like to cover inheritance and inheritance is one of the main concepts of object
oriented programming, and it allows a class to inherit features such as properties and methods from
another class and reduce them.
So the structure is the following you have a subclass and then you have a super class.
Alternatively, you can call the subclass child class or derive class, and the super class can be called
parent class or base class.
Now let's have a look at those in action, so let's create a new class called vehicle.
Now, this class might have some properties and some methods.
OK, and now we can go ahead and take this class and inherit from it.
So this means that we will take its properties and methods and we will make them reusable.
So let's go ahead and create a new class called car, which is of type of vehicle which inherits from
vehicle.
If you do that, you need to make this class open, so vehicle needs to be an open class.
Otherwise, I cannot use inheritance here.
And that is because all classes in Courtland are final by default, which means they are non inheritable
by default.
So you need to use the open keyword to make a class inheritable, and that's what we're doing here.
So we're using open class vehicle.
Now, let's say I want to have another class which inherits from car, so class electric car that should
inherit from car.
Now problem is car is not open, so I would need to make it open as well.
All right, so if you want to make sure that a class cannot be inherited, you can use a sealed key
word, then this would not work.
So you cannot inherit from a sealed class and you can even see that converts your subclass to an object.
And I'm just going to get rid of that because I want to make it open so that we can reuse it.
Now, this year, our vehicle is the superclass or parent class or base class, so you can use either
of those terms.
Each of them is fine.
Now, this year, our car is the subclass of the vehicle class.
And of course, it's also not just a subclass, but also the child or derived class.
OK, so it's just different terms, but whenever you come across those terms, you should know what
they are, right?
And then at the same time, our car is to superclass parent class based class of vehicle.
And of course, in that case, the electric car is the surplus or childless or derived class of the
car class.
OK.
So.
Let's get rid of vehicle now because I want to only work with car and electric car for now.
So let's say we have some properties in here and let's also say that our car has a primary constructor,
so we want to have some more value in here.
Well, name will be a string value brand will also be a string.
So this car has a name and they brand.
And then here, electric car, suddenly we have an error here, so we need to add a constructor parameter
name.
As you can see here, name is now required and something else is missing.
The constructor parameter Brent is missing.
So you can see.
This is how Courtney expects it.
So you need then to use the same parameters that were used for the car, for your electric car as well.
So there's just a constructor that we're using here, the primary constructor.
Now, of course, the electric car can be extended, so you can add more features to it.
It has to have at least two features of the class that it inherits from, but it can extend features
so it can have additional features such as, for example, battery life.
So an electric car would have battery life, which is of type, let's say double.
So that is the base structure when working with constructors.
So with primary constructors.
Now, let's say we have a ranger here in our car, so let's create this class car first here range.
It's going to be of type double and it's going to have a value of 0.0 just as an example here.
Then it has a function in here fun extend range, which will be the amount which will be of type double.
And if the amount is positive.
So great zero.
Then I want to increase the range by the amount.
OK, so if we.
Go to the fuel station and we fuel up our car and the range will be extended, so we extended the range.
And then finally, I want to have another function here fun drive.
So distance should be double.
And I will just print here drove for distance kilometers.
OK, now I have this class car and of course, our electric car inherits from it, so that means that
if we now go ahead and create a fund main, so our main function and in here I create an object off
car.
So far, my car is type car.
Now, of course, I need to add a string, so I'm going to give it the name, for example, A3, which
is of the brand already.
And then I create a electric car, so my car, so I'm going to call it, is going to be an electric
car.
And here it's going to be Tesla S or maybe s model of type Tesla.
And then it of course, needs also to have a battery life.
So I'm going to give it 85.0, which means 85 kilowatt hours.
All right, and the cool thing now is let's go ahead and use my car, don't drive.
And we will see that it needs a distance, so let's say drive for 200 kilometers and here, of course,
it's a double so 200 to two.
And now let's do the same thing with my car that drive and use also two hundred kilometers.
All right, so if we run that now concede it doesn't complain, even though there is no drive method
inside of my electric car, there's actually nothing in mind extra car class.
It's using nothing.
The only thing it does is it inherits from the car class.
So let's run it and test what the result will be.
And we can see that it says drove for 200 kilometers and drove for 200 kilometers.
So in both cases, it drove for 200 kilometers, even though electric car never implemented a function
called drive.
All right.
But it can still use it because it extends the functionality of its parent class, so it has all of
the features of its parent plus.
It was also this feature called Extend, so we can also go ahead.
Maybe we should rename those.
So that's going to be the Tesla S and Audi A3.
And let's reuse those names because I think it's more telling to use those names.
Now you can see that our Tesla can drive for 200, but our Audi can also drive for 200.
Now let's extend our duration or the range of our Teslas.
So Tesla is not.
Concede has extended range does name range brand to try to string all of those, so it has several.
Functions which we have never created, for example, hash code equals and to string.
And these are the ones that are always available.
These are always available.
Why are they always available?
Because any class inherits from the any.
Class, so I say it again.
Any class inherits from the any class.
So there's a class called any, and we can use this type any as we've seen before.
So for example, I can create an object bar object.
One is of type any.
Okay, so and he's a class in Scotland.
Let's see it.
It's an open class, the route of Scotland class hierarchy.
Every Scotland class has any.
As a superclass.
And that's why we can always use the function or the method equals hash code and to string.
And can overwrite them as well, we can create our own types of those.
But now let's go back and use the extend range here.
So let's extend the range by 200.
So now we extended.
The potential distance that our Tesla last can drive by 200 kilometers.
And that is because we have this fun extend range in our car class.
Now let's go ahead and let's say we want to override the range functionality and we want to override
the drive functionality.
So what we can do in our class electric car, we can just go ahead and say override bar range is battery
life multiplied with six?
So let's say with 85 watt hours and we can drive something around five hundred and ten kilometers or
so with this range.
So it's more or less realistic.
I think maybe it's just five, but something around that five or six times the battery life in kilowatt
hours is what a Tesla can drive.
So now we are overriding the range property.
Now, of course, it's complaining.
It says, OK, you cannot override range because the range is not open, so you can make.
A property open as well.
So here we can use open for this property called the ranch.
We can use the same feature for a function.
So now we made this drive function open as well.
So now we can go ahead and overwrite that functionality so we can say, for example, in our electric
car, that we.
Drove his override, fun drive, and we say we drove a certain distance, but we also print line.
Say something like drove distance on electricity, but that by itself wouldn't be as interesting as
doing the following so we can confuse the whole thing.
Let's go ahead and say drive.
And now we just print something like drove range kilometers on electricity.
So now what we have is we have a drive function, which we override from our parent class and we have
a drive function, which we created inside of our inheritance.
Which we created inside of our subclass.
This might not always be a good idea, because it might end up being confusing, but in the end, now
you can go ahead and say Tesla start drive.
And you can see you have two options here, so you can either drive or you can drive a distance.
So let's say we drive.
So before we used to distance feature and now we just drive because we want to know how far a Tesla
can drive and if we run that again, then we should be able to see the maximum range of our Tesla S.
And we can see that we get 710 kilometers on electricity here.
So we have created an overriding function, so we are overriding the functionality of our superclass,
so it would change whatever was written in here.
And then we create another function, which is now specific to our electric car, which has the same
name, but it uses different features.
Of course, we can have a bunch of other variables in here as well, so we cannot just have the variables
that are used in our.
Superclass here for the electric car, but we can also have a charger type, for example, let's say
different electric cars have different charger types or something like that than we can say, Okay,
this car has a charger type of one.
And if you want to change dad, we can of course do so for every single Tesla s that.
Charger type, and let's say it has a type two charger.
All right, and there's one thing that I should say that what we're doing here with these two.
So let me put that a little further down.
What we're doing here is called poly more, because at least that's part of polymorphism.
Polymorphism is the ability to treat objects with similar traits in a common way, which we are doing
right here because a Tesla has common traits, because it's inheriting or it's of type electric car
and it's inheriting from car.
And this, of course, only works because electric car is a subclass of car that would alternatively
also work if it was a subclass of car, which was an interface.
OK, so if car was an interface, that would work as well.
But what interfaces are is something that we're going to look at in another lecture.
All right, so that was a little introduction into inheritance, and let's see some more examples of
that, for example, with interfaces and the next video.