Welcome back to the next tutorial. In this one, we're going to talk about method overriding. So what method overriding is, let's say there's a class present, and there's a method inside it. Now I've got I create a class called B. And now my B class extends the class, which I previously mentioned. So which means my class A is a parent class to class B, and Class B is a child class to class A right.
So now there are a lot of methods available in child in the parent class, which is Class A. And the class inheriting that class. If that rights the uses the method with the same name as they were presenting the parent class, then it is known as overnighting of letters. So let's do it practically. Let's start talking and bring these things into action. So I have this class called cars overriding parent.
So this is the name. So I created a method called engine start. Let's give up something I don't even need this argument. Maybe we can say in cylinders, maybe how many cylinders it has, and all those things. So let's do this and print something that I am. let's print something.
So, I am from the pain in class. So because this is the method implemented in the parent class and mentioning that I am from the parent class, so when we call it, we know that this is coming from the parent class, right. So, now I created a class call. This is the name cars overriding child BMW. So, this is a child class that's going to extend my parent class and we just named a child, BMW. So now we'll use the let's use the keyword extends.
And this extends the class called cars over riding. parent class. So now it overwrites the parent class, right? What I can do is guys, but I can use the same name right? So if I Do the same name. If I implement the same method, let's copy this save, save some time.
And change this. I'm from the child class. So now this method is from the child class. So what we just did is overriding. So we overridden the method that was defined in the parent class by this method. It's the same name.
And it's defined in the child class. So that's what we call as overriding of a method. So we can use this keyword as well, annotation as well if you want. All right. So we can use this as well actually, but this is not required or mandatory. It's for good coding practices, like Java would recognize that this method has the same name, so Java would fix those things.
But for good coding practices for us to recognize those things later, it's good to write those keywords. So now if I let's say, run this method, and let's let's name so let's create cars. We're knighting child BMW and then create an object new. This right? So now let me call this thing. So see when that engine start and give it a integer something because it accepts it expecting integers.
So we just call them metal. And now if I run this class to this, it says, I'm from the tag class. Did you guys notice that? This never called the parent class method? Right? So now it has overridden this.
All righty. So if you see the console, it says I'm from the type class, right? What I can do is, let's say I want to use the functionality that was already there in the parent class, let's say this. And then I want to add some more functionality to my method. So what I can do is I can use this keyword, super keyword. So this is the this is a very important keyword in this.
So what I can do is super dot engine start and then let Too fast too. So what it's going to do is now first it's going to call the method call engine start from the parent class, because it's the super, which refers to the parent class. And then it's going to perform all the functionality in the child class. So if I run this method, now, it's going to do something like that, that first call the parent class method and from the parent class, and then call the method from the tech class and from the child class. So what are the advantages of method overriding? Guys?
So the main advantage of method overriding is that the class can pretty much give its own specific implement implementation to the method, right? So if I have multiple methods and multiple methods, default implementation in my parent classes, but in my child class, I want to do something more. I'm gonna write a specific implementation to some method, like my BMW Cars do something else. When they start the engine, they start with different things. They have different cylinders. They need few different things, right?
So I can override that functionality which was defined in my opinion class. So that's what the use of the main advantage of overriding is right. And then the second thing is, what we can do is we can actually assign the parent class or friend to the child class object as well. This is called dynamic method dispatch. So by using this method, what we can do is we can enable to assign the parents class reference to the child class object. How we do that is, let's say I do something called maybe, let's let's put the cars overriding and the parent one let's let's create the object like this.
And then new cash payment, right? Something like that. So now let's create one more method inside it with something like public wide and increase speed, something like that. We're just putting some dummy methods. So I am the Crazy p from the parent stocks, right? So this is a new method that we created in the parent class and I'm the increased feed from the parent class right.
And now let's again remove this super keyword, or maybe I'll just commanded for you guys not remove it. But we can do is we can call this method there. And we can call See, when that increase speed, right, we can call this method. So now let's run the code. So if I run the code now, what happens is, the first one says, I am the increased feed from the parent class. That's true.
And this is I am dying please, I am from the painting class because what we did is we actually created the instance of the class so but what we can do is here we can write cars, or riding child BMW. So if I do that, what I'm doing is I'm assigning the parents class reference to my Take us if I do that, let's save it and run the code. So it says, So, with the same object, we are able to call the methods from both the classes from the parent and the child. So this is I am the increased feed from the parent class. So this method, call this the increased speed method. And now, when I call even dot engine start, so, this is the same object right, and it's calling a method from detail does I am from the child class, this this engine start method.
So, now, it didn't call the Indian start method from the parent class, even though we gave the reference type as the parent right you guys can see that, but this is the type class object that we created by giving the reference of the parent class this thing is known as dynamic method dispatch. So, what we can do is with the same object, we can call the methods from the parent class and we can call the methods from the child class all together at the same time. So, this is one very good advantage of doing that already So this is all said and done. But there are some rules to overriding. So I have written these rules here for you guys reference. So feel free to read them out.
And it's really important, but I'll go through these things. So, the important thing is arguments list and the overriding method should be same as the parents class overridden method, what does it mean? So, arguments list in this method, right? This thing should be same as the method in the parent class. So it accepts one parameter, right? one argument.
So if I try to say something like, string, one more variable, something like that, right? instantly tries to show me an error, right? It says, The method engine start of type cars overriding BMW must override or implement a super, super tight method, right? So since we just changed the arguments list, it starts complaining. So that's what the first point means that the argument list should be same. They can not I'd be a different argument list if we are trying to override a method from the parent class into a child class.
Now, the second thing is access modifier. Now, what does it mean? access modifier of the overriding method cannot be more restrictive than the overridden method. So let's say if this method is public, and I'm trying to override this method here, so I cannot make it private. So if I try to do something like that, let's say do this, again, it starts complaining it says, cannot reduce the visibility of the inherited method from cars overriding parent right? So I cannot reduce the access modifier.
It has to be public, because it was public. But let's say if it was protected, let's say this raw tected right, if it was protected, and I declare here as public it is possible. So the constraint is we cannot reduce the rest activity. But if we increase it That's fine if it was protected there. And if we make it public here, that's completely valid. So we can do that.
Now let's come to the last point, which is private static and final methods cannot be overridden. And vice versa, because we know that static and private and public and the final methods, they, they belong to the class because they're local to the class, right? So we cannot override those methods. But what we bargain what we can do that is, if we have a private methods, let's say we have a static analysis static, so this is increased p, right? And maybe not this, let's make a new method. So public, and maybe let's call it Boyd, static, static example, and something like that.
And see so the static methods This static, right, right from the painting class, all said and done. So this is a static method, it says summers or swing. So syntax looks bad. Somewhere public static void, it should be, it should not be Boyd static. So it's done and it's good. So if I have a static method, I will not be able to override that.
But what I can do is I can have my own implementation of the static method. So I can create my own method, I can, I can simply just call this. So I copied up here and put it this here and name it as let's say, tile class. And so what is done is, it's not overridden. I have created the same name. I have created the same name, same method static, but it's not overriding.
It's just the same thing because if I try to, if I try to do super and static example But I will not get it right. So if I try to call the static example from the superclass, I will not get it, because that's a static method. And that's belong to the class. So what I'm doing here is I can, I can use the same name, I am writing my own implementation of this with the same name, but I'm not overriding anything. It's not going to override anything from that method. So this is what we can do.
So these are some rules, guys. Just keep them in mind. And I'll say, practice, practice, practice as much as you can. And thanks for attending this one guys. Stay tuned for the next one. Thanks for thanks