Welcome back.
In this video we're going to implement and use the interface in our project.
So we have this AI animal that we created last time.
And now instead of using inheritance as we did, we're going to use the interface in a new class that
we're going to create.
So let's create the just underneath so that it's easier to see.
So I have this public class dog that will implement AI animal.
Now you see I directly get an error where it says does not implement interface member AI animal makes
sound and dog does not implement interface member AI animal eat string.
So I need to implement these two methods so I can just hover over it and just implement interface or
implement all members.
So I'm just going to say implement interface.
So you see here it automatically added the code for me with eat where it gives me a string food and
with the make sound where it's just a void method.
And it made both of them public.
It did implement some code inside of it, but I get an error throw new not implemented exception, which
is pretty cool because this forces the developer who who is using the interface that we created to actually
implement it.
Otherwise we're going to cause this program to crash and then the developer will know, oh --, I
forgot to implement this method, so I forgot to actually add code into the method.
Okay.
So that's the thing about interfaces.
When you are using them you need to to implement their methods.
So let's go ahead and just use this dog to eat.
So I'm just going to say dog eat.
And then whatever the food is plus food okay.
And then for the make sounds method I'm just going to say bark okay.
And the dog is just barking.
So now I have implemented the I animal interface.
So now let's use it inside of our program class here.
So I'm just going to say dog dog new dog.
And I'm going to make a sound.
And I make the dog eat.
So dog dot eat.
And what should the dog eat let's say a treat.
So treat like so now let's make sure that we can run this code.
And I'm just going to add a read key here and run it.
And we will see it says bark and dog ate treat so very well.
So you see it's actually quite simple.
The execution here is rather simple.
But there's a lot more under the hood.
And we're going to dive a lot deeper into why, where and when using real world scenarios where this
will make a lot more sense.
Because in this case you might say, well, why would I care?
Why would I do that?
The thing is, you have to consider that these interfaces, while you can make your own interfaces,
very often what you will do is you will just use a bunch of interfaces that were created by somebody
else.
So you don't have control over these interfaces.
But then when you are implementing those interfaces, you also need to implement its members, and then
you need to follow certain guidelines and rules.
Right.
So that's where it really comes into play.
It's not so much about making them yourself, even though once you become a more advanced developer,
you will start potentially using interfaces as well.
But it's on one hand that.
But on the other hand, it also allows us to use multiple inheritance, which we're going to do later
on in this chapter.
So see you in the next video.