‫In the last video you learned about inheritance and the basics of inheritance.
‫But now let's dive a little deeper and look at several key words that will be relevant when it comes
‫to inheritance.
‫And the ones that we're going to look at will be virtual and the override keyword.
‫Now, you might wonder, why do we have animals here on the screen?
‫Why does Dennis show a bunch of images of animals?
‫Well, the thing is, we are going to implement an animal class which will then allow us to inherit
‫from and animal will be the parent class, which has a couple of skills, such as, well, what can
‫an animal do?
‫It can make a sound, it can eat and it can play.
‫That's at least what we are going to allow the animal class to do.
‫And of course, an animal can do a lot more.
‫We'll give it a couple of properties such as the name The Age Property, then if it's hungry, for example,
‫so only if it's hungry, it will eat, otherwise it won't eat.
‫So that's really the idea of creating an animal class.
‫And now you could, of course, go ahead and implement this class by yourself already.
‫And then we're going to use inheritance in order to not only use the same methods and same properties,
‫but also override them basically saying, okay, the sound that an animal that is a dog makes is going
‫to be different to the sound that, for example, an ape makes or a monkey makes or a giraffe makes
‫or something like that.
‫So this is basically what we're going to implement.
‫And by doing that, we are going to learn the different key words such as virtual and the override keyword.
‫So let's go ahead and create a new project for this.
‫I'm going to use a console application.
‫I'm going to call it virtual override demo.
‫All right.
‫As a little challenge for you, if you want to try this for yourself, please write a little class that
‫will be called animal and it will have the properties.
‫Name age is hungry.
‫Then of course a constructor, then a method that is called make sounds, eat as well as play so positively
‫and implement all of those if you want to, because this will just give you a little bit of a practice
‫to create all of these different methods.
‫And then we will see how to change them a little bit up so that we can use them properly in inheritance.
‫All right, so I hope you tried it.
‫Therefore, of course we can use control k c to create a new class and I'm going to call this one animal
‫like so.
‫And now this animal class will have a couple of properties, as I said.
‫So the name property, the H property as well as the is hungry property and then a simple constructor
‫that uses all of these properties.
‫You can see we're just using get set.
‫By the way, if you want to create a property real quickly, just enter property double tap and then
‫enter the type, for example, string and then here, for example, name.
‫So that's how you would do it very quickly, not having to type more than necessary.
‫All right.
‫So this is going to be our constructor here with the name the H and the is hungry.
‫And you can see is hungry is not something that we are going to set to the animal once we create it,
‫but we're going to set it to true at the beginning by default.
‫So I'm just setting it to its hungry default.
‫Okay.
‫So now I said that I want to have a method that will be called make sounds.
‫So let's look at this method.
‫So I'm using an empty virtual method, make sound for other classes to override.
‫I'm making this method virtual so that it can be overridden by classes that inherit from animal.
‫And we're going to see later on how to do it because the sound that an animal makes will highly depend
‫on the animal itself.
‫So being hungry is something that every animal is sharing because, well, that's how we function.
‫We need energy and we get that through food.
‫And in order to motivate ourselves to get food or to eat food, we have the sense of hunger.
‫But this virtual method that we have here, this makes sound, doesn't have to be empty.
‫So let's look at this eat method here.
‫And this eat method will also be a virtual method.
‫This allows our subclasses to override this method.
‫So if we want to override the eat method, we can do so, but we don't have to.
‫The general functionality will be the one that you can see here.
‫So it will check if the animal is hungry.
‫If it is, then it will just print out the name and say that it is eating.
‫And then if it's not hungry, it will say name is not hungry.
‫So the name is, by the way, the property of our animal.
‫So this name, property of the animal.
‫Okay, so that is going to be our method and we're going to make it virtual as well.
‫And then another virtual method which will be to play.
‫So this virtual method play which will basically say name is playing on the console.
‫Now we're using some very simple red line statements.
‫This could be, of course, a lot more complex code.
‫So this is just to simplify the whole thing and make it more humanly understandable, so to speak,
‫because playing and eating and stuff, this is something that we can directly understand because we
‫have seen it in our lives and we have done it ourselves while as reading data from a database or something
‫like that.
‫That's not something that we usually see in real life.
‫Okay.
‫All right.
‫So now let's define our classes that will be derived from animal.
‫So that will inherit from the animal class.
‫So let's go ahead and use control K and C or command K and see.
‫And this will then open up the screen where we can then create a class or alternatively just create
‫a class.
‫Otherwise, so I'm going to create a dog class and this dog class will inherit from animal like so.
‫Now you can see already that there is no argument given that corresponds to the required formal parameter
‫name of animal name string int.
‫So we need to create a constructor here.
‫So let's go ahead and create a simple constructor which will follow the same structure.
‫It will have the name and the age and it will have well use base name H so that we can use the same
‫constructor that we have defined that animal.
‫So now I'm going to use this new property called Happy or is Happy and this will be a boolean.
‫This will basically just store if the animal in this case the dog is happy.
‫So it will say true if the dog is happy and dogs are happy animals.
‫So by default, the dog will be happy.
‫All right, now let's override the method.
‫So let's simply override the virtual method, eat.
‫And therefore we're using this override keyword.
‫So public like a normal method, but we're adding this override keyword.
‫So override, void it.
‫Same to you.
‫Or similar to what we had here where we also had this method, but here we used a virtual keyword.
‫So this virtual keyword allows our subclasses, our inheriting classes, so the children to use the
‫override keyword to basically override the functionality of it.
‫If you want to still call the base functionality of the parent class of the animal class, in this case
‫we need to use base, dot it base will say, okay, call the method of the base and the base will be
‫well you see here animal.
‫So once you double click on base and it's highlighted, you can see that animal is also highlighted
‫because animal is your base class.
‫If you keep the control key held and you click on base, it will jump over to the animal class because
‫that's the base class.
‫And you can see there are three references of our base class and that is here this base as well as this
‫base and this animal here at the top.
‫Okay.
‫So at this point, we are basically using our base class here, the base class functionality, we're
‫not overriding anything.
‫So basically we're saying a dog is the same way that any other animal would.
‫So nothing special here.
‫Now, let's look at the make sound method, because this one is going to be quite different because
‫the dog has to make sounds right.
‫And this way of making sounds is going to be woof.
‫It could be also woof or woof or whatever.
‫However, your dog likes to make sounds, and this is how in my case the dog will make a sound.
‫So this will be different to every animal.
‫So every animal will make a different sound.
‫And that's why we are overriding our make sound.
‫Well with well, this makes sound that we had here.
‫We are overriding it with our individual implementation for our dog.
‫And now let's do the same thing with play because the dog only wants to play if it's happy.
‫So let's assume that that's the case.
‫So here, if we look at play, it says Dog is playing, but I'm not happy with this result.
‫Let's say a dog will only play if it is a happy dog.
‫So what we can do is we can override, void, play and check if the dog is happy and only then play.
‫So we have already overwritten the base animal functionality with the functionality well with our own
‫functionality for the dog or our unique functionality for the dog where we added the condition.
‫But this could be anything.
‫You are not limited to your condition.
‫This could be like an entirely different kind of implementation.
‫You don't even need to use base play, so you're totally free to use whatever you want.
‫For example, in the make sound method you can see we're not using base make sound because well, in
‫the make sound method there's nothing to be called, so there's nothing interesting here.
‫But even if there was, we don't have to call base, make sound or base, make place play.
‫We could implement something entirely different here if we wanted to.
‫So that will be our dog.
‫Now, let's actually go ahead and save those two classes and work with them in our program, CSS file.
‫Okay.
‫So here, let's first of all create a dog like so I'm going to call the dog, which will be a new dog.
‫Which will have the name of CIF and the dog will be 15 years old.
‫So pretty old dog.
‫And now.
‫We can write something like so we can access the property of the dog like so.
‫So red line dog name.
‫So we can get the name of the dog and the age of the dog using dog dot name and dog age.
‫And then we can, for example, play and eat.
‫Okay, let's do that real quick.
‫Don't play dog eat.
‫So let's run this.
‫And we can see Sive is 15 years old.
‫Safest playing, surface eating.
‫Okay.
‫So play and eat work.
‫Totally fine.
‫Now, of course, we can also use Dog Dot.
‫And make sound here.
‫So this would then make the dog sound.
‫Let's run this.
‫And you see the stock, says, Wolf.
‫All right.
‫So let's quickly recap and look at how this is different to the example that we had in the last video.
‫So we have our base class and this base class has a couple of properties and it also has a couple of
‫methods, functions, so to speak.
‫So here to make sound, for example, the E method and so forth.
‫And we make them virtual by adding the virtual keyword in order to make them changeable or override
‫able in the inheriting class.
‫So dogs is the inheriting class or the subclass.
‫And this subclass allows us to now override a specific method from the base class using the override
‫keyword.
‫So that will now.
‫Override the functionality.
‫So it will replace the functionality or the code of the current method.
‫Except you of course use this based on it.
‫So what that will do is it will basically just call the method of the base class, which means of the
‫parent, but you are free to implement your very own code here.
‫You don't have to call the base class.
‫You can of course add conditions so that the base class functionality would be called in a specific
‫condition.
‫Or you can write whatever code you want inside of your overriding methods, and then something else
‫is different.
‫And that is that we are adding code to our constructor here for our.
‫Subclass.
‫So this inheriting class dog is on one hand using the same properties that our base class is using,
‫but at the same time it has an own implementation.
‫What should happen once a dog is created?
‫So once a dog object is created?
‫And in our case it is just to say that the dog is going to be happy.
‫All right.
‫That's it for this video.
‫We're going to look a lot more into inheritance, of course, because there's still a lot to learn.
‫So see you in the next video.