 Hello, this is Christian. Welcome to episode five of this video series. In this video, we're going to do a little demo to put all these together, the classes, trades, and interfaces, and how they are used in a program. So let's go and take a look. So in the IDE here, I'm going to create, I know I call classes, but that's OK, I guess. Let's go and create, let's just say there for our example, I want to create a class called Plants. So I'll call the plants file. OK, and also, I already have animals. I'll use this animal to compare with the plant. And let me just collapse this. And I will use this two screen to show you what, well, not that way, vertically, I guess. We can close this one here. OK, so we can compare between the two here. So on the left side, let's say I'm going to use the plant. And then on the right is Animal Kingdom. So for now, let's say I'm going to create my class for the plant first. So I have a class kind of similar to the animal kingdom. And I'll call it Plant Kingdom. OK, and then this plant kingdom has something in there. I can also make this as an abstract to kind of match what I have over there. And then I have a class called the actual plant. I guess we call it, I don't know what, Tree. That is, well, I mean, Tree is a real plant. Let's call it that spruce to be more specific. And that it will extend the plant kingdom. OK, so here we go. And then I can create an object here, P for plant. Yeah, well, S for new spruce. OK, so I have two objects kind of similar to what I have over here. And these are just constructors. So let me turn these off. And so we don't have to show any of those stuff. And this one here is something else. I want to turn that off and remove that. So they look very similar to what the other plant class is. OK, so we have two separate sets of classes. One is for the plant kingdom. One is for the animal kingdom. And so what is the point here, I guess? I don't have an interface here, right? So we could have an interface to create something that is very generic that the animal and the plant kingdom must implement. So how do we do this? So we can create, let's put it on the animal side. Let's create an interface. We call it just I for interface. And I'm just going to call it type. I don't know, I'm just making this up, OK? And so let's just say inside here, I have a function called set type. And also I have a function called get type, right? I'm just making this so we can use it. And so it's an interface that both of these animal kingdoms will use. And I'm going to implement that as well. Implements the I type. And then this also implements that I type. So now both of these have those interfaces. But now they have to implement that, right? Implement those functions because they both use it. So in this case, I have to implement both of those, which is required. You can either do that or you can implement inside the abstract class so you don't have to do it, right? So I should have put it in the wrong place. Implement type. Yeah, that's correct. All right, so then because they're required, I can do inside here in the abstract class. So I don't have to do it down here. If I do it in here or in here, you see that I have to do it both of them. And depending on how these functions are used, OK? If they are used differently, then that's great. If it's not, then it's redundant. But you have no choice because you are stuck with it, right? Because you have to use it. So therefore, I'm going to go and just put here. Implement the methods down here in my Animal Kingdom class, and the Mammoth class. They're in here now. And let me turn this off so you can see. I'm going to turn this off just to show that they are in here. I'm going to just collapse these. So we have this implementation. The Mammoth is happy. They plant the spruce tree. It must also implement those to make it happy as well. So here they are. And then they are both good to go. OK, so you can see how interfaces can be played in this scenario. It's something that can be shared across two different classes now. Now let's say that in the plant kingdom has a really important function called, maybe, again, I'm going to put public here, function something like what should we call it, determine age, the age of something, the age of the plant, or whatever that is. So its function can also be used to determine the age of the animal. So I say, I'm just using one example here. It could be a bunch of functions, a useful functions that the Animal Kingdom can also use. So instead of recreating these functions in here again, these are for implementations. So the interface, they are not implemented. So you have to do that yourself manually inside here for each subclass. But let's just say that the Animal Kingdom, the plant kingdom has some functions that are really useful. And I want to use those in the Animal Kingdom too. So I don't have to recreate them again, because what they do is very similar in nature, or exactly the same. So what do you do? How do you solve this problem? Well, I don't want to rewrite them here. So you can say, OK, well, maybe I can then make another class, another class that can use and share these functions in both classes. So you can say, OK, well, then let me go and create another class up here, maybe another abstract class called species. So I put all these functions in the species class, and then now this plant kingdom can implement the interface, but also can extend the species class. So you can see how that works. Let me kind of minimize it a little bit smaller, but I apologize. So here's the same thing. So if I want to use those functions, I can just, again, Animal Kingdom can extend this species class. And so now I have both have access to the same functions in the species class, so you can extend that as well. So my mammoth can have x to that. If you go in here, type m dash, you will see they have the determinant h. Absolutely. Same thing over here, s, and then have determinant h. So I solved that problem. Cool, huh? OK, so and then another scenario will be, say, you have another class out of somewhere, another class, and this is nothing related to species at all, or the Animal Kingdom. I'm just going to make this up, say I have another class called math, some kind of math class. And this math has a really important function called special class, special function, in my case, special calculator. That is really useful for both of these species, or the plant kingdom, and the Animal Kingdom here. OK? So you want to be able to use those, and so you can say, well, OK, because I can extend these classes. Of course, if you extend this species class, if you extend that to the math class, then problem is solved. Of course, if you do that, and you can see that down here, I have s points to my special calculate class. Same thing with m over here. You saw already a special class. There you go, problem is solved. So this is fine. But let's say you have some restrictions, or you don't want, because this math class has a lot of functions, how do we do this? So let's just say you don't want to extend this class to the species class. You don't want the species to extend this math class, or you are prevented from doing that. Or let's say that you don't have access to that extension. So you cannot extend that class. So the species cannot extend, because, again, you are given this class as a species class. As this function, you can use it. You can extend it to use it. But you also want to get this class from the math class. And since you cannot alter the species class, it's locked. It's not public to you. And you really want to use its functions in here. Well, you either have to create them yourself, or you have to forego one or the other. And this is where traits come into the picture. Traits come in place. So of course, this class, I assume that you have your own implementation. So you can implement this math class and include these functions inside the plant kingdom. Yeah, the plant kingdom. This is your kingdom. And so you have these functions. And you also want to share these classes, or these functions, and the member of class 2. But you can't, because you can only extend one class at a time. So you have limitations. So therefore, you can say, OK, I want to turn this into a trait. So the trait can work very similar to another class. So with a trait, you can then use that inside either the spruce or the plant kingdom if, let's just say, these are your classes that you create. And this is the only class that you are not allowed to create. You can extend it, but you cannot touch it. So you can put it here or in here, depending on which level you want to use. Let's say I want to use inside the plant kingdom. Then here, you would just go and use it, use the math. So now I use that in here. I can also use an animal kingdom. So you can see that now I have access to all the functions in the species, the functions inside the math, and implement my interfaces all together. So now, and then any other class, any other kingdom that belongs to the species, maybe don't want to use the math class. They don't have to, because it's not part of the species. And if it's not useful to that class, then you don't have to use it. So have a math class. You can have another trait for something else. For example, you can say trait, I don't know, service. I'm just making this up, OK? And the service has a public function that has a really, really service. IT, of course, has no relation to this, but has a really important function you want to use. And so, OK, well, it doesn't apply to the species it does apply to the animal. So you add that to your animal kingdom. So here we go. I have borough some functions in the math, service, and the species all together to my mammoth. So my mammoth now has access to all of them. So m has, you can see, has the service IT, has all the other functions that are special calc, but I did that already. So it gets pretty messy here, but at least you can see where traits are, how they play, a role in your program. And interfaces are as well. Of course, when you design this program, if you are to design a developer, you have this, you had some designing to do. And if you are the user only, if you're given only these interface or these species classes, abstract classes that you are required to use, then you must implement any function in the interface. You might implement any abstract functions. If I have an abstract functions in here, abstract function called solve, for example, that you must implement those in your subclass as well. As you can see here. And again, just the rule. So you have no control over that. And if you want to add more functions from another class or something class you wanted to create yourself and you want to share those classes with other classes that you own, then of course, trait is the solution for that because you can either, again, in this case, forgo the species. Or if you want to use them, then you have to create traits to provide other services or functions to your class. Of course, our assignments will probably won't ever get to this part, but I just want to give you a look up ahead in case if you ever run into a large project and like this scenario. I mean, it saves you a lot of typing, a lot of redundancy, because you don't want to recreate all these functions that are very similar or actually exactly the same in your classes. So I hope that at least gives you a picture of what the complexity of these are, but of course, the power of using classes, traits, and interfaces in your program. So let me know if you have any questions. Thank you.