Welcome back and this video we are going to look into interfaces and interfaces are a feature which
allow us to extend the functionality of classes.
So, for example, our car class could extend the functionality of an interface that has certain features
and lets go ahead and just create such an interface.
So I have this interface prepared here, which is called driveable.
It has one value, which is a double value called max speed.
Then it has a function called shrive of type string.
So it's going to return a string, but there is no implementation of the function, so it just doesn't
have a function.
Body only has to head of the function and then we have a brake function which actually has a function
body as well.
So an interface may provide a default implementation of some or all of its properties and functions,
but it doesn't have to.
And if a property or function has a default implementation, the class that uses this interface may
choose to override it.
What does that mean?
Well, let's say we have this interface driveable right in there.
We have these functions and this property.
Now, if our class car decides to extend the functionality of the driveable interface.
Then it needs to implement the property that is created in the interface but is not implemented.
So here we need to go ahead and say, volle, make speed of type, double should be in there.
And because we're overriding something, so we are overriding the speed from here, we need you at the
key word override that by itself doesn't make our class happy.
Yet, as you can see, it's still not happy.
Still complaining something is still missing.
And that is because we haven't implemented the member called Drive.
So here's this drive function.
And because it doesn't have a functioning body, we need to create one in the class that extends the
interface.
So we need to go ahead and create a drive function.
So you can see we are already creating a drive function.
Why is it still complaining while this drive function doesn't follow the same structure?
So you can see the head of the function doesn't return a string.
So let's go ahead and actually do that.
So override fun drive and once again, fun drive and you can see it gives me some help here to idea,
but it requires me to implement something.
So I need to add a return statement here so I can of course, go ahead and return.
Driving the interface drive.
OK, so that's just going to be the text that I return.
Alternatively, what you could have done is you could have done it in a shorter way, for example,
just saying.
That it's going to return whatever that string is.
OK, so both of them are fine, I can see this version is also fine, it's just a shorter version of
this here.
All right, so now we have multiple drive functions, and that is already starting to become confusing,
maybe, but no worries.
The one driver that we have here is actually the drive that comes from our interface that we are extending,
OK?
So.
You can see here, disk drive, we implemented it and suddenly our car is not complaining anymore,
then going back to our other drive that we have created, it's an open function drive, which is, so
to speak, new.
So this is the lowest level in which it's defined and that is in our class car.
So this is another drive which we, of course, can override from other classes that inherit our car.
Now, if Electric Car wants to inherit from car, it also has to follow the structure of our.
Interface here, so let's also add.
A mixed bit.
And let's use it here as well.
Now, of course, our function drive that we had in our electric car that we created doesn't work anymore
because now this drive.
Is trying to override so things that we are trying to override the drive function.
Which comes from here, this one.
OK, so we also need to follow the structure of drive.
Now, let's add the overwrites keyword, but then we also need to return to spring time and of course,
here return.
And we can use the same structure that we had before, or at least the same strength that we are returning
here.
So what is an interface?
Well, an interface is essentially a contract that a class may choose to sign.
If it does, the class is obliged to provide implementations of the properties and functions of the
interface.
OK, so we see that here, because our class car is extending the interface driveable, it's now obliged
to implement its.
Variables and functions or its properties and functions.
The properties, we did that by using it in the primary constructor and to function well, we implemented
it directly here.
You can also see, because our break had already an implementation, we never had to use it, but we
can, of course, use it.
We can even use it and a subclass here.
So in the subclause of our car, in the electric car, we can still go ahead and implement brake.
So Kansi override brake and you can see the super keyboard and that is to call the brake function of
the superclass.
And because our superclass, which is our car, didn't implement the functionality for brake, it will
use the one from the interface.
So it will just use this brake here.
So now if I go ahead and call a break and by the way, now of course, we are missing things when creating
those are the three and so forth.
And I think it was speed.
Right.
So 200 kilometers, that is the top speed here.
And 240 is going to be the top speed of the electric car and out the errors disappear.
But now which drive will be called when we call this drive?
Well, if we look at it, one requires a distance.
The other one doesn't.
So it's going to call this drive.
And then this year, of course, going to require the distance.
It's going to call.
They strive here.
All right, now, let's say we want to call the brake function here.
All right, let's just test it.
Let's go ahead and use Tesla as the brake.
And let's use Aldy a three to break.
And here, of course, it's breaking.
So I'm going to get rid of the other calls here just to keep it simple.
And let's actually run it here.
And we will see that it says the driveable is breaking in both cases, so the case for Tesla and Aurier
three, even though while we're already a three, which is of type car, never actually implemented
the Strache function, OK, so has it, because it automatically inherited from brake.
Now.
What if we implement something here, let's say print line, break inside of.
The electrical.
Let's test it again.
So you can see the driveable is breaking and that is our test, the.
And then it says, break inside of electric car is also our Tesla, and then we have this driveable,
this braking, that's our Audi that breaks.
So what is happening?
Well, when using override fun break, it's super brake.
What happens is that the super function brakes or whatever was implemented for brake in the interface
or in the superclass will be used or will be executed.
And then on top of that, our own code that we add afterwards will also be executed.
OK, so that is what this super thought method name does.
By the way, subclasses of a class that implement an interface in this case are electric car are also
considered to be implementing the interface.
OK, I said it in a certain way, but I've never said it specifically like that.
OK, so just so you know, our electric car is also implementing the interface called.
DriveAble.
Another site notes an interface can inherit from another interface.
So, yeah, in case you want to create your own interfaces that inherit from other interfaces, you
can do so.
So why would you even use interfaces?
Well, interfaces are super useful when you have certain functions in mind that you definitely want
to be implemented later on and also already know the properties of a class that you want to then have
implemented.
But you don't want to implement them directly.
You just want to prepare everything.
And you don't want to, for example, create a specific body already for the function.
All right, so this is the introduction to interfaces, and in the next video, we're going to look
at abstract classes, which are similar in a certain way to interfaces, but they have some differences
in the nuances as well.
So let's have a look at those.