Hello, in this Java programming video, we are going to take a look at overriding in the context of classes. So for example, we've got this super class what is parent class and it has a method call. And the enemy class that extends the or you know, inherits the kinds of life and property can use the method from within or, you know, the objects of it can use it sounds super enemy, but maybe super enemy that we go to and maybe secret doing whatever damage you apply to it, can you know, he can't, or maybe cuz think about Okay, so I said by default, you do it as damage. You just take away damage. So currently we have quite a health initially. So instead of just doing regular damage, you want to do twice as much as 10 times the health but there'll be twice Man damage.
And what you can do is override the method. So you just put the exact same method. So if I have to just copy it just to keep it simple. I'll put it here. And instead of this time to Now, what this means is the method been overridden. So, if you try and access the reduce off method of the super enemy, though, inherits from the character class, it will use the overridden method.
If you can't find an overridden method, it will go to the parent class. If you can't, you will use this one and that's demonstrated. So we warm Monday's comment. This is quite a bit of code. And I'm going to create an enemy, only one enemy and I'm going to do system and then and I'm going to print E one dot L then I am going to do e one dot reduce health I'm going to reduce by one and then I'm going to print out the health against right away afterwards then I'm gonna do something very similar, but for the frequently so for a fun of me and so if we run it, see what we get, we get 199 obviously, it's been constructed as well we can ignore that, you know 199,998. So when we created the enemy class, then we printed out the help which was 100.
Initially, we did reduce how we pack In a value of one printed yet, so we had reduced it by one, then we created a super enemy object, we printed out the health and by 45, because that's what we set in the constructor. When we call the reduce health method and pass in one, even though we only plasti know the value, one super enemy says, whatever the value is, double it, that's what times to those, double it and take away from the health. And that's what we got. We got 998 Is there a 999. So that's, you know, that's really for override it. And this is really useful, because maybe you have parents last year, let's say five, six children classes, and maybe four of them use the exact same method, exact same way.
But the fifth one, you know, you did it slightly different and now we can override it. So it gives you the best of both worlds. Let me just show you one of the things and this is useful in a variety of places. Classic. And that's the super keyword if I go to the Java. So if, for example, I would comment that out and run it, I see we still get a siphon.
If instead I would use dot reduce damage, let's see what we get 999. But what had this super keyword does, so super is sore like that this keyword, this keyword refers to any properties of this class, including any inherited properties were a super refers to, you know, the parents properties. So this is invoking the original produce health method. So if you want to do that, maybe you actually want to, you know, invoke both of them. If you really want to do that you can, so it's taken away, the default amount of one and the or the amount, as well. So that's the super keyword.
You Really useful. So that's the way that you can invoke the parent properties directly instead of having any over rhythms. So that's it for overriding. If you have any questions, feel free to reach out, and I look forward to seeing you in the next awesome Java video.