4.13 Example Method overload
In the previous video we saw the concept of method overloading in this course let's do some practice to better understand this concept, let's start by creating a method calculates Score, it will allow us to calculate the score of the player there will be two parameters here the first will be the name of the player in string a second parameter will be the score we display the name of the player, the score and we will return the score of the player multiplied by the number of points, we just did that to be able to return something is because we defined the method to return an integer so just to confirm that it works we can type the name of the method as we saw before, we can pass a character string as parameters as we defined in our method and do not forget to put the Guillemet just put a number with 500 points say here the team of players to score 500 We run our program in our terminal, we will have the name of the player and his score, to have the final score we must put our result in a variable since our method returns an integer value. we will create a variable int named scoreFinal which will contain the final score let's display now the final score, the name of the player and his score Let's move on to the overloads of the method now. As explained in the previous lessons, it's about overloading methods, that is to say using the same method name, but with different parameters, so we'll start by creating a second method and in fact I'll simply copy our first method and paste it, notice that when I do that, first of all, we have an error and it says calculatesScore with a String and an int as parameters is already defined so Java will tell you look you can't use an identical method with an identical number of parameters, there is a problem So the first option would be to put an s here, that is change the name of the method, and of course that will work, but then it's a separate method we're not really overriding the method that we're using, so in that case the second option that's most adaptable to our problem will be to change the number of parameters so I can get rid of the first parameter, I make this method have only one parameter and notice that the error usually disappeared, we now have an error on line 18 because we are trying to access a variable that no longer exists in this particular method I'll just remove the player name in our method Now we have two methods with identical names and we have a warning on the screen, but it's not an error, it's just the name of the method that is grayed out compared to the one we declared before, it's just intellij saying look you created this overloaded method called calculateScore but you're not using this particular method while you are using that first one which is in orange and you can see that we are using that method in line 7 so we can still run it we still get the same results, so to use the second method that is overloaded what we need to do is call it those same process for all the method calls that you have made if your IDE is intellij, intellij is showing us that we have two methods here and we can select the method that we want to use here, I'm going to select this method that we created and overloaded that has only one parameter