What are Interfaces? (C# Basics)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we're going to learn what our interfaces in c-sharp this is an excellent feature that helps us write cleaner more reusable code by defining a set of methods that we can then implement in various classes let's begin [Music] hello and welcome I'm your code monkey and this channel is all about helping you will learn how to make your own games with enough tutorials made by a professional indie game developer so if you find the video helpful consider subscribing so interfaces are one of the main features of c-sharp and writing object-oriented code essentially they allow you to define functions and properties but without any implementation the interface works as a contract it ensures that when a class implements that interface they must implement those functions this allows you to have several different classes that can be used the same way by using a common interface and you don't care what else that class is doing maybe it's a tiny class that only has the interface functions implemented or maybe it's a mega class with a thousand lines of code when you use it as an interface you can guarantee that it will have those functions regardless of what the rest of the class is doing alright so let's check out some basic code first and then we're going to look at a practical example of how we can use interfaces so that our player here can shoot both the enemy the building and the crate this video's patreon sponsor is bad adventure bad adventurer is a game development duo currently working on their first game Wayfarers edge it's a RPG focused on exploring and settling unknown frontier lands in a low fantasy and Wild West theme check them out at bat Adventure calm thank you to the patreon sponsor and thank you to these awesome supporters for making this video possible go to patreon.com/scishow code monkey to get some perks and help keep the videos free for everyone so here we are in an empty script now the first thing we need is to define our interface now we can define it in a separate file or in this case just write it in here so first we write our accessor so let's make this our public and then instead of using the class keyword we use the interface cubed then we write the name for our interface now standard practice is to start the name of an interface always within an eye so in this case and let's name it I my interface and that's it just like this we have our interface definition now inside our interface we can define a function and now normally when defining a function inside of a class you would do something like this but this is an interface and not a class so first of all in an interface you don't define an accessor by default all of the interface elements are public so get rid of the private then we have our return value so in this case void is correct then the name of our function and finally the parameters then again the interfaces do not have any implementation so we get rid of the entire code mark and just put a semicolon one side note here in c-sharp eight they add the ability to add a default implementation so if you're easing c-sharp eight you can actually define an implementation of your method that won't be used if the implementing class does not define that method this allows you to add extra methods to the interface without breaking existing classes however c-sharp eight is still very new and not yet support the immunity so in this video I'll stick to not using default implementations but if you're watching this in the future and c-sharp eight is a standard then default implementation is yet another useful feature of interfaces okay so just like this we have to find a method in our interface we don't define the accessor since by default everything in an interface is public and we don't find any implementation so now let's see how we get a class to implement this interface down here let's define our class so let's make it a public class my class and now here we have a con and then our interface I my interface and just like this we can see that we have an error since we are implementing this interface we are required to implement all of the functions defined in this interface so let's do just that first of all the accessor which again with interfaces it must be a public then the function signature so void test function and with no parameters and finally we implement our method body just like this okay so now we have this class which correctly implements our interface now let's try it out let's go up here for our testing and first let's instantiate the new my class so we have an instance of myclass and now let's make a function that has a parameter of our interface type so this function receives a parameter of type I my interface and then here when we access my interface you can see that we have the interface function right here so we called the function on the interface and now up here after we have instantiated our class we can now use our test interface function and here it expects say I my interface object and we can pass in my class since my class does implement that interface okay so let's test running out this code and see what happens and if there it is we have our my class test function log so it correctly printed our message so up here we created an instance of myclass then we're passing in my class as a parameter of type my interface and then we're using the interface to call the interface function which is calling the function on the class that implements our interface and running this line of code again interfaces our contract so that means you must implement those functions and you can also have multiple classes implementing the same interface but with different implementations so let's try it out and make another class and here we're also going to implement I'm my interface and now up here let's create an instance of my second class and we call the same test interface and pass in my second class so we have a function which receives a my interface and in here we're sending it with two different object types and if we test and yep there it is both of our classes implement the same interface so we can call the same function on both of them but they implement the interface in different ways so we get different results awesome so here we define a simple interface method now inside of our interfaces we can also define properties so for example in my int then we the get and with set so here we have a simple property however we cannot define fields so then this and you can see interfaces cannot contain fields now another thing we can define our events so just like that so these are all the things that you can define in an interface you can define functions and events and you can define properties but not fields now in terms of implementing an interface it can be done with a class like we saw in here but we can also implement our interfaces instructs in the exact same way one of the most useful things about interfaces is how you can implement several interfaces when dealing with inheritance one of the main limitations is how you can only inherit from a single base class however with interfaces you do not have that limitation so here we can define a second interface and over here on this class we can implement on my interface and also I my second interface so just like that yet another limitation of C sharp is how structs cannot inherit from baseless but again structs can implement interfaces also an interface can implement another interface so for example I can make my interface implement the second interface now on here when I implement just on my interface you see that I have to implement also the other function so this means that whatever class implements the final interface it must also implement everything else defined in this one and in this one alright so here we looked at the theory behind interfaces now let's check out a practical scenario okay so here I am in the Nicene I have my player character that I can move around over here is an enemy character and over here is a crate now I can left-click in order to shoot some bullets and now what I want is for the bullets to damage both of these objects here in the same you can see both objects and you can see that they are both completely different so this is an enemy with the enemy script and this is a crate with a great script these two classes are completely separate they don't share any base class or anything so this is where interfaces are extremely useful let's look at out the scripts are set up over here in the project phones we have a bullet a enemy and a crate script let's inspect the boat there it is oh it has is this the bullet works by using physics so it gets fired and here we're checking for collisions so it's in here that we want to cause some damage if we hit a valid object then over here on the enemy script we already have a very nice damage function and the crate has one as well now for this type of shooting a bullet and testing for the hits I cover this in a previous video so check the link in the description if you haven't seen it yet and I'll back in the boat script over here we can identify what type of object we hit so we go into our Collider and we do a gift component and let's see if it's an enemy so you do get component and if enemy is not know then we have hit a enemy there is just like that and then we can do the exact same thing for our crate so can either get component that type crate and just like this so by looking at this code you can probably guess where we're going with this but let's test ok so here we are and I can shoot my boats and if I aim towards the enemy and shoot and if there you go we have a nice hit and over here is a crate aim and shoot and yep there you go I've hit the crate ok so we have our code working we can actually damage both types of objects but now let's say we want to add another object type something like a building doing it this way would mean that we would have to add all of our possible damage object types all in here in this function so eventually this would become a massive horrendous function so this is where interfaces come in instead of copy pasting all of this code for every single object type what we can do is let's go into the editor and here let's create a new C sharp script and let's call this AI damageable this won't represent an object that can be damaged so inside let's go and make this a interface called eye damage of all and on we're going to have is a void damage function that's it very simple now let's go into the enemy class so we have our enemy we are extending monobehaviour and now let's also implement i damageable and now we already have the damage function so we don't actually need to write anything else and the same thing on our crate so in here just implement I damage one all right so that's pretty much it now if we go onto our boat now instead of having all this copy pasted code we're going to go into our compiler to get the component of time I damageable so this will return an object i damageable see if we have it then we have hit a damageable object and we can call the damage function on our interface and that's it let's test okay here we are shooting bullets let's shoot the enemy there you go he got hit should the crate and there you go it got hit alright so we can now target both types of objects and now let's add another object type so over here I have a tower and it's holding the tower script and right now if I shoot it you can see nothing happens okay and now I can simply open up the tower script and just add damageable and that's it it already implements that function and now if you run so here we are and shoot any of there you go we can now damage the tower so just like this we have another object and made it damageable by just implementing a simple interface and you can also see how the implementation doesn't have to be exactly the same for example in the enemy here when I shoot him he spawns blood particles but the tower and the crate do not so over here you can see a very nice practical use case of how interfaces help you write cleaner better code instead of having lots of copy paste itself we just made sure that the damage works based on the interface and not on the specific object type another recent example is how I use interfaces extensively in the video on making a modular character controller in that video each module is defined as an interface then there are various implementations of each interface so for example there is an interface to set a target move position and then two classes that implement that interface one goes directly towards the position and one uses path fine so now that you know the basics of interfaces go check out that video to see how they can be used this video is made possible thanks to these awesome supporters go to patreon.com/scishow to code monkey to get some perks and help keep the videos free for everyone as long as you can download the project funds in a tony's from unity code monkey calm subscribe to the channel for more ENT deterrents post any questions I have in the comments and I'll see you next time [Music]
Info
Channel: Code Monkey
Views: 78,833
Rating: undefined out of 5
Keywords: unity c# interface tutorial, c# interfaces tutorial, unity interface, c# interface explained, c# interface, code monkey, brackeys, unity tutorial, unity game tutorial, unity tutorial for beginners, unity 2d tutorial, unity 3d, unity, game design, game development, game dev, game development unity, unity 2d, unity 3d tutorial, programming, coding, c#, code, software development, learn to code, learn programming, unity c#, c# tutorial, unity clean code, c# clean code
Id: MZOrGXk4XFI
Channel Id: undefined
Length: 13min 53sec (833 seconds)
Published: Sun Apr 12 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.