4.6 Method Overloading

Video Activity
Join over 3 million cybersecurity professionals advancing their career
Sign up with
Required fields are marked with an *

Already have an account? Sign In »

4 hours 19 minutes
Video Transcription
hi and welcome back to the course. I hope you guys are doing well. And if you remember in our previous lecture, we did a lab in which we implemented the idea or concept of method overriding by creating a parent and child class as well as a sort of tester to see how both classes work and how this concept of method overriding works.
And in this video lecture,
we're going to be going over the concept of method overloading. Now, before I start in the last videos and slide,
it said method Sorry, Module five, method over loading. That should be just method overloading were still in module, for that was a error and it husband fixed. It was just mentioning that to make sure that you don't get confused and we're still in model for So that was just a small mistake that I fixed
moving on. We have a few learning objectives that I'd like you to keep in mind when we go go throughout the video,
The first point that we're gonna be
covering is the general high level idea of what method overloading is.
Then gradually, we're gonna move on to understanding why we would ever have the need off overloading any method or using this idea of method overloading.
Finally, I'm gonna talk about some differences between method overloading and overriding, because that seems to be a point of confusion for a lot of people, including programmers.
So what is method overloading to begin
now? If we start with the formal definition method, overloading gives you the ability to give a method or several methods the same name as a pre existing one, but with a different method. Signature.
Now if we talk about method signatures, a method signature in Java consists of the following items, a name
and the number type as well as order of its parameters.
No, I'd like to discuss some advantages of method overloading.
The first advantage that it ah comes along with is three idea to call a similar method for different types of data. Since each method can have a different return type
and then it encourages cleanliness of code
as well as readability of code.
Now, why would we ever want to use method overloading?
So the first point over here really sums that questions answer up. Let's say we have a class in which we are doing some sort of mathematical calculations and we have a funk, a method or function called,
Um, calculate. Now let's say that calculate method, um, adds two integers together.
And what if we want to use the same name of that method but add two decimals together or subtract to introduce together? It would perfectly make sense to use the same name calculate. But then we would have to change the signature of our method if we want to use the same name.
Now moving on, we're gonna be discussing some differences between method overriding and method overloading. So
here are some key differences. First method overloading increases readability. Overall, it ah is done within the class on Lee. So nothing external
and then the parameters of each method that you're overloading must be different.
And finally, changing the return type of a method doesn't count as overloading that method.
If we move on to the method overriding side, it is used mainly to provide a specific sort of implementation of a certain method,
and it occurs in two classes that have something called a is a relationship.
The parameters must be exactly the same,
and the return type of the method that you are overriding must be the same. Or this ordinary must be a subclass of the type returned by the method in the parent class.
All right, so two summer up, USAA To sum up this video lecture,
we went over the concept of method overloading
and the need of method overloading. Why would ever find ourselves overloading a method? And that was exclaimed through the math calculate method example.
And finally, the difference is a few key differences between method overloading and method overriding.
Now, in the next video, we're gonna be doing our second lab of this module, which is called math overload. And as you can tell by the name, we're gonna be doing some method overloading, and we will actually see how this concept is implemented in a real world program.
Now, as always, if you have any questions at all, feel free to contact me. The Ellington and I would be more than happy to any to answer any of your questions. Thank you very much for watching. And I look forward to seeing you in the lab
Up Next