‫In this lesson, you will learn how to create and implement your own interfaces with a concrete example,
‫and you will get an idea of why interfaces are used, especially when our classes don't have a relationship
‫with each other and therefore they don't share a base class.
‫So inheritance as such is not an option.
‫Imagine that we are working on a video game where the player can destroy stuff, but everything we destroy
‫will have different behavior depending on what is destroyed.
‫For example, destroying a chair will play some destruction sound and will spawn destroyed chair parts.
‫But destroying a car will play an explosion sound, create fire and destroy other objects nearby.
‫For our game, we have the following classes a chair that inherits from class furniture and the car
‫that inherits from class vehicle.
‫So let's look at those classes real quick.
‫So we have this chair which inherits from furniture, and the chair just has a simple constructor here
‫where we set the color as well as the material based on what is given to the object that is created.
‫But now, of course, this class is inheriting from furniture, which is why we have those properties
‫here, color and material that we just used in the chair class.
‫And then we have the constructor here, which is the default constructor, where those values will be
‫set by default.
‫So the color and the material will be white and wood by default.
‫And then we also have a simple constructor which uses the color as well as the material to set them
‫by default.
‫Okay, so there is nothing to fancy, but of course we also have a car and we want to be able to destroy
‫the chair and the car, as I stated earlier.
‫So let's look at the car real quick.
‫So here we have the same story, a subclass car that extends vehicle where we set the car speed as well
‫as the color.
‫So now we are using, by the way, a simple constructor as we do it here.
‫But of course, we could have used based here as well, passing the speed as well as the color.
‫So that is also a valid option and we wouldn't have to use this in here.
‫So there are two different ways, of course, to go about it.
‫Okay.
‫And then this car is inheriting from vehicle where of course, speed and color have to be defined.
‫So let's look at the vehicle class and we can see here that this base class vehicle has a speed, a
‫color, a default constructor, which sets those two values, speed with a 120 kilometers per hour and
‫the color with white.
‫And then we have the simple constructor which set the speed and the color based on the values that are
‫given to the vehicle object.
‫Also, of course, to the inheriting objects such as a car, for example.
‫Okay.
‫So now that we have all of those different classes and we saw how they are structured, let's look at
‫the actual destruction feature.
‫So what is the best approach to create such a destruction feature?
‫If we think about it, we can't add a class to make our classes inherit from it because C-sharp is a
‫single inheritance language and not a multi inheritance language we all already inherit from one class,
‫so the car already inherits from vehicle and the chair already inherits from furniture.
‫So it cannot inherit from something else.
‫Like, for example, the vehicle here, you see, C-sharp doesn't allow this because it cannot have
‫multiple base classes.
‫A class cannot have multiple base classes in C-sharp.
‫So plus there is no concrete relationship between a car and a chair to share one base class anyways
‫because they are entirely different to two things, right?
‫If we implement normal methods called the destroy method, for example, in each of those classes that
‫we have here, it will be hard to maintain.
‫What if we decide to change the destruction system entirely or add visual effects to our destructible
‫objects in the future?
‫It will be a pain to actually go through each class and modify it.
‫Because in this case, we only have two subclasses, so to speak.
‫Right.
‫But imagine you have 50 or even 100 different classes, 100 different types of objects in your game.
‫Then you would have to go through every single one of them and adjust the code accordingly.
‫So that is really a burden and we don't want to go through that.
‫That's why we need to find a better solution.
‫What we need is a way to enforce our distractible behavior on every class that uses it, just like a
‫contract.
‫And if you have watched our video on interfaces, you know that they are contracts that we can create.
‫So the best approach in this case is to use an interface and we will call it I Destroy Able.
‫And any class that implements this interface will be forced to follow our destruction requirements and
‫customize it at the same time, depending on the class itself.
‫So let's go ahead and create such an interface.
‫Therefore, you can go here to your project, add a new item, and then select interface right here
‫and give it a name.
‫So I'm going to call this one.
‫I d destroy ble so you can see when using interfaces you add the I at the beginning of the name of the
‫file to indicate that this is going to be an interface.
‫So let's add this interface.
‫So this interface will just have a property to store the audio file that will be run once we execute
‫the destruction, which will be the destruction sound.
‫And then we just have a method that will destroy an object and we're going to call this method destroy.
‫It will not return anything, it will just destroy something.
‫So it could, for example, return the graphics that you have.
‫So the objects that you want to display once the item is destroyed, for example, the car is destroyed
‫or the chair is destroyed and so forth.
‫So let's now make sure that our classes are in fact going to implement this interface and then, of
‫course implement the interfaces content, meaning the destruction sound as well as the destroy method.
‫How can we do that?
‫Well, let's go over to our car, for example, and here our car will need to add a comma here at the
‫top of the class definition at the top and use this I destroy able interface like so now if you add
‫that you can see that you get an error which says car does not implement interface member destruction
‫sound as well as destroy so we can show potential fixes and implement all members.
‫For example, this will create this automatic code for us.
‫So it will create the void, destroy, able destroy and it will create a destruction sound for us.
‫So that would be the auto generated version, but we can also do it manually.
‫There's no problem with that.
‫So here, for example, we can implement the destruction sound.
‫And then what I want to be able to do is I want to have a property to store the destroyed objects nearby.
‫So when a car gets destroyed, it should also destroy the nearby object.
‫And this list is also going to be of type.
‫I destroy able, which means it can store any object that implements this interface and we can only
‫access properties and methods in that interface.
‫So in order to create that, I'm going to create a public list of I destroy all objects and I'm going
‫to call them destroy balls nearby.
‫So now we can go ahead and extend our constructor.
‫So on one hand, we want to make sure that we initialize the sound.
‫So we make sure that we have an explosion sound.
‫For example, let's say we have this sound called car explosion sound amp three.
‫And then we want to initialize the list of destroy able objects.
‫So this will, for example, just be a new list of destroyed objects, and then we just need to also
‫implement the destroy method.
‫So this I destroy.
‫Well, here at the top you can see it's still not happy, it's still expecting us to implement the destroy
‫method.
‫So you see when you implement an interface you need to implement its members because the interface here
‫has two members, we need to implement both of them.
‫So that means that we need to go back here and we need to actually implement the destroy method.
‫So let's really quickly do that in order to do it.
‫What I want to add in here is going to be, first of all, two statements.
‫I'm just going to use right line statements.
‫I'm going to keep it simple because we're not creating a game here, but just trying to understand the
‫concepts of interfaces.
‫So when a car gets destroyed, we should play the destruction sound and create a fire effect.
‫So here we are just mimicking as if we would actually play the sound and it will just say, okay, playing
‫the car explosion amp three sound and then we create the fire.
‫So everything is under fire.
‫Everything is burning because the car is destroyed.
‫So now let's go through every single destroy all nearby and actually destroy it.
‫So here we're going to go through every single I destroy object that is inside of our list of destroyed
‫balls nearby.
‫We call it destroy all.
‫So every single item is going to be a destroyed ball, which is basically just a local variable name.
‫So we say, okay, the destroyed object that will implement the ID Storable interface will just be called
‫destroy ball in our for each loop.
‫So we iterate through it, which means we iterate through the collection, destroy balls nearby, which
‫is a list of I destroy objects, basically objects that implement the ID stretchable interface and then
‫we just call the destroy method.
‫So basically we create a chain reaction here, destroying everything around everything that is destroyed
‫and that is also a car and so forth.
‫So in this case, let's say we have two cars next to each other and they have a bunch of chairs next
‫to each other.
‫So the cars will call this destroy method where they will destroy the items nearby because, well,
‫it's a huge explosion.
‫Right.
‫There is a sound coming up and every item creates a sound.
‫Now, you could, of course, create a timer that it needs until the actual explosion happens after
‫something catches fire.
‫But we're going to keep it simple here.
‫So we're destroying the other cars, but also the other chairs.
‫So each car will infect other items surrounding it that are nearby.
‫And this could be, let's say, everything that is ten meters away or five meters away or however many
‫inches away if you want to take the American system here.
‫But that is basically what's going to happen.
‫Now, let's look at the chairs, because the chairs are going to have to go through a similar approach,
‫because we also want to make sure that a chair is destroyed, but the destruction of a chair will be
‫very different.
‫So here we will also add the interface.
‫So the I destroy all interface and of course we need to implement the destruction sound and that will
‫be a very different destruction because the chair will just fall apart and it will be shattering wood
‫that you hear.
‫So it will be a very different sound here.
‫And then of course, we want to define the destruction sound inside of our constructor as well.
‫So I'm going to say that the chair destruction sound should be played.
‫It will always be the same.
‫That's why I'm not adding it here in the declaration of the Chair.
‫So I'm keeping it simple here.
‫Of course, you could have different chairs that have different explosion sounds.
‫Then you would add it here at the top as a parameter for your constructor.
‫Okay, so now let's actually implement the destroy method.
‫And in this case, what will happen is we will destroy something and we will, first of all, destroy
‫the colored chair based on the color that the chair has.
‫We will write that, then we will play the destruction sound.
‫And then finally we are going to spawn chair parts.
‫So in our case we're just writing it onto the console.
‫But just imagine in a real game how you would do that, because the best way to destroy something in
‫a video game is not to actually do the physical thing where you do all of the destruction, which is
‫very difficult to build as especially if.
‫Don't have a big team that does all kinds of things.
‫Then you would basically just make a big explosion or destruction animation, and then you would spawn
‫the destroyed parts of the individual parts that lay around.
‫So you see here there is no chain reaction coming from this destroy method.
‫So the destroy method implementation for the chair is very different to the one from the vehicle, but
‫both of them inherit from the I destroy able interface and have to implement the destroy method and
‫they can do it their own way.
‫So basically interfaces allow us some sort of multi inheritance, even though it's not really multi
‫inheritance.
‫Okay.
‫So now let's go ahead and go to our program, CSS, and actually use our newly created classes because
‫well, otherwise it's boring, right?
‫So let's go ahead and create two chairs.
‫So we have an office chair which is going to be brown and plastic, and then we have a gaming chair
‫which will be red and wood.
‫And then let's also create a new car.
‫So this car will have a speed of 80 and it will be blue and it will be a damaged car.
‫And then we're going to add the two chairs nearby, this damaged car.
‫So we add them as destroyed wheels nearby to our damaged car.
‫So the office chair as well as the gaming chair will now be, if we look at it here as destroyed, it's
‫nearby because when creating a car, this list will be empty.
‫But then we can, of course, because it's a public list here, we can add items to it, which we do.
‫We add destroyed all items to it.
‫And why can we add chairs to our destroyed balls nearby?
‫Well, because a chair implements this interface.
‫That we called it tribal.
‫This means that a chair is now going to be accepted as an object of type I destroy.
‫Which means we can now add it as our disturbance nearby.
‫So now we can go ahead and destroy the car.
‫So let's just call the destroy method for our damaged car so the explosion can take part.
‫The explosion sound can start and all of the good stuff.
‫So let's run this.
‫And of course, this will be more of an adventure game and not so much a game where you see actual 3D
‫graphics or anything.
‫So we play the destruction sound, car explosion sound, we create the fire.
‫The brown chair was destroyed.
‫We are playing the destruction sound of the chair destruction sound amp three.
‫Then we are spawning the chair parts for our brown chair.
‫Then the red chair was destroyed.
‫We are playing the destruction sound of the chair destruction sound amp three and then we're spawning
‫chair parts for our red chair.
‫So you see, that's pretty much it.
‫So that's how you can create your own interfaces and how you can use them.
‫And I hope that you now understand what interfaces can do for you and the advantages of interfaces.
‫So an important note here by interfaces are used for communication between two similar or non similar
‫classes which do not care about the type of class implementing the interface.
‫Just like how our car communicated with the chair to call the destroy method all because they just implemented
‫the same interface as I earlier stated.
‫So while interfaces might seem hard to understand at first glance, they provide us with great perks.
‫Those perks are code readability.
‫So an interface constitutes a declaration about intentions, so it defines the capability of your class.
‫So what your class is capable of doing, if you implement the AI sortable, for example, you are clearly
‫stating that objects of your class can be sorted then code semantics.
‫By providing interfaces and implementing them, you're actively separating concepts and interface defines
‫a behavioral model, a definition of what an object can do and separating those concepts keeps the semantics
‫of your code a lot clearer.
‫Then you have the code.
‫Maintainability interfaces help reduce coupling and allow you to easily interchange implementations
‫for the same concept without the underlying code being affected.
‫And then we have design patterns.
‫It's the bigger picture of using contracts, abstraction and interfaces pivotal for object oriented
‫programming, so human understanding and complex system architecture.
‫So it's really basically just to understand it better as a human being and then multiple inheritance.
‫So interfaces can be our gateway to use multiple inheritance in C-sharp.
‫So suppose you write a library and want it to be modifiable by users.
‫You write interface and its class implementation.
‫Other developers who will use your library can still write their own implementation class, which may
‫use different technologies or algorithms that achieve the same result.
‫This is also why we meet so many interfaces in libraries we use, but rarely feel the need to write
‫our own interfaces because we don't write libraries ourselves.
‫On a final note, the important thing is to understand how interfaces are implemented at this point.
‫Of course, as we go through different technologies like WPF and ASP.NET and the C sharp master class,
‫we will start using interfaces more, which will help you understand them better in the future.
‫All right.
‫So thanks a lot for watching this video.
‫See you in the next one.