11. How to program in C# - INHERITANCE - Beginner Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
thanks for tuning in at brakus hello and welcome to video number 11 in the how to program in c-sharp course today's video we are going to take a look at inheritance basically deriving classes from other classes and what inheritance means we'll get into in a second but first of all I just want to explain that inheritance isn't an extremely important subject and it's considered one of the three pillars or primary characteristics of the object-oriented programming language because basically what inheritance allows us to do and I've made this horrible looking graphic here but it will do for now is it will allow us to share code between classes basically if you have a base class imagining that we are making an RPG and we have the space class called our hero and he has different properties that are general for all heroes such as a a health amount a may be a strength amount a amount of intelligence and all of that and then we have some derived classes that basically share all of those properties but also add on some new ones so a derived derived class could be a shaman and he might have some abilities and another one could be a warrior he might be able to yield a specific kind of weapon all those kind of things can be derived from the same base class and then basically what we do is we instance the derived class so when we then change this instance it's not going to be the base values are not going to be changed for all of the instances that's not the point the point is just reusing the code so let's try this out so let's open up examine studio and I've started with a clean slate here and we're going to start up by creating our base class and we do that just as with any other class and we are going to call this animal stick to the same that kind of idea that we've been using so far this concept of an animal that then has some different properties and that's going to be a public string which will be the name we're not going to default it to anything a public public integer which is going to be the age in a public float which is going to be the happiness I think these values will be pretty general and will pretty much apply to any kind of animal so other than that we can also give it a method so we're going to do a public law right and this method is going to simply print the base values so this could also be something that alters maybe we could call this age to make it actually age and so simply just add one on to the AIDS value we could call it be happy and we could add on to the happiness value we could print out only the name we could do a bunch of things with this but for now we are simply going to print all of the base values and in here is simply going to do a console dot write line name and then plus your name we're going to do console dot write line H whoops H and then the H and finally we're going to write out the happiness cool so this is going to be our base class and you will notice that we don't give this a keyword to say that this is our base class and the reason why is we don't really need to specify that classes can derived from other classes that derive from other classes so the term with it a base and a derived class is relative so next up we're going to create our derived class and this is going to be called dark and it's going to derive from and therefore we do a colon animal like this now the dark class will share all of the base values here or the base fields with the animal class so inside of this we could maybe do a public integer then this is just what we want to add on to it and this could be on the spot count the number of spots that it has on its body and also we could do maybe a method in here just to show that that will work too and we're simply going to call this bike and what this will do is it will simply print out a wolf awesome and we can something to just copy this change this to a to a cat which is also going to derive from animal and this is going to have a different property here or the point wouldn't be so this could maybe be a float of cuteness and I know that dark you can be cute of a hump but stick with me here and this is going to have a meow method and then in here we simply do turned out yeah cool so basically this is kind of the base of inheritance what we can do now is simply use this so it's it's very easy to use I mean it doesn't require that much more syntax but it can really help organize your program make it easier to read minimize errors and definitely minimize redundancy you don't want to do the same thing over and over and inheritance will definitely help you on that I mean this is central to many different kinds of programs you can write an entire program without using inheritance though that would normally be not a very good idea I mean if you're making an RPG you cannot get through that without using inheritance so now inside of our main method we can start instantiating these derived classes so now we have if we go back to our graph we have a base class called animal we have a derived class called cat and another one called dark and I will instance these and we'll see what we can do with that so let's start out by making a dark and we're going to call this one spotty and we go set him to in new dark and first off let's set his name whoops spotty dot name equal to spotty will set his age equal to what are we going to do we are going to say four years old you're going to set his happiness to 0.8 and next up we can access the values that are specific to this derived instance so the spot count and you will notice that we of course can't access the cuteness because that's specific to the cat but we can access the spot count and we're going to set that equal to 25 hence his name and finally we're going to say spuddy dad oops Spidey dad and we are going to print a space and then we can maybe I make him bike so if we just test this and hit play here oops of course we need and it's just going to say that this is going to always have a default value of zero because we're not using it and that's on your warning so we can just ignore it for now we're going to be using it in a sec so for now we can just do cancel that rekey here so it won't quit immediately so let's try that again and you can see that it prints out the names body is four years old he has a happiness of 0.8 and he can say woof great so let's see let's say that inside of this derived class we want to access one of the parameters inside of the animal class so basically we can do this by simply typing and this is very easy personally typing happiness and now we've accessed that parameter so it's simply going to do that for us but let's say we had a value of happiness here and a value of having us here but don't know why you would do this but sometimes you need to do this if especially this can be customized if you're passing in some arguments and you want to set the base class through the derived class and then this can be a normal thing but basically if you have happiness here and happiness here you can make sure that this is the one you're accessing by doing base that happiness and some people like to do this even though it's not necessary to be very to be very thorough and show that the value that we're accessing is not actually a member of this class but of the base class so if we say that we want our happiness to go up with zero point one every time we wolf then we can simply do that there and then we can print out down here we can print out his new happiness and that's going to be equal to spotty done happiness now that we've parked so let's try that again and you can see that his happiness goes up with zero point one so we've accessed and changed a value in the base class so let's just before I just to show you this let's make an instance of the cat also and we are simply going to call this Heisenberg and we are equal it's going to equal him to a new cat we're then going to say Heisenberg dot name equals Heisenberg Heisenberg dot h equals and we're going to make him old so I'm going to live 13 his happiness is unfortunately not too high it's an old grumpy cat and his cuteness is not too high either so that's maybe why he's grumpy and then we're going to print out the base and we're going to make him do a meow comb and in in between these sesame want to make some space here so that's just Megan empty blind they're awesome and and this arrow should go away when we run and you can see now that it first prints out all of the data about thee and dark and then it goes on to print out data about the cat so that was basically it for this tutorial it was kind of a quick one compared to some of the others but I thought that I would really just encapsulate this in one single video I mean there's much more to talk about when it comes to inheritance and how it can be used especially when it comes to kind of protection levels but I think that this is what we're going to stick for stick to for now I don't think we're going to need to kind of delve into more until we get a little more advanced with c-sharp so that's basically it for this video I will just quickly go through exactly what happened what is happening here if you haven't got it if you feel like you've got this covered you can just skip this so basically what we're doing is we're creating the base class called animal we are giving him some different properties and we are making sure that these are public so that we can access them we are also making a method called print' base that will just print all of the values in our base class then we are creating a class called doc that derives from the animal based class this basically takes all of these parameters and copies them in at the top here including our method and what this will allow us to do is some of the add-on to these values a spot count and a method for backing and we do the same with the cat it basically derived so from this class meaning that all of these are just covered in here and then we add on a cuteness level and a method for saying meow then down here we are creating an instance of the dark we are naming this buddy we're filling out a bunch of different parameters for spotty we're then printing out his space we are backing and then we are on checking our new happiness so when we run this and you can see the result of that and that's basically all the stearic so that was it for today's video I hope you enjoyed it and I'll see you in the next one
Info
Channel: Brackeys
Views: 220,585
Rating: undefined out of 5
Keywords: unity, unity3d, tutorial, beginner, easy, function, and, settings, how, to, howto, learn, learning, course, series, tips, tricks, tutorials, workflow, fix, tip, technology, game, development, develop, games, programming, coding, basic, basics, C#, inheritance, derive, deriviation, base, class, classes, value, properties, fields, Unity (Software), Computer Program (Literature Subject)
Id: EiBCF7rYRtI
Channel Id: undefined
Length: 14min 8sec (848 seconds)
Published: Mon Jun 29 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.