Presentation is loading. Please wait.

Presentation is loading. Please wait.

DAY 23 5.2 OVERLOADING.  Overloading: allows for making several methods with the same name (method heading) which differ from each other in the type.

Similar presentations


Presentation on theme: "DAY 23 5.2 OVERLOADING.  Overloading: allows for making several methods with the same name (method heading) which differ from each other in the type."— Presentation transcript:

1 DAY 23 5.2 OVERLOADING

2  Overloading: allows for making several methods with the same name (method heading) which differ from each other in the type of the input and the output of the function.  It is simply defined as the ability of one function to perform different tasks. OVERLOADING IN CODING (  RHYMES)

3 For example, doTask() and doTask(int income) are overloaded methods. To call the latter, an integer must be passed as a parameter, whereas the former does not require a parameter, and is called with an empty parameter field. A common error would be to assign a default value to the integer in the second method, which would result in an ambiguous call error, as the compiler wouldn't know which of the two methods to use. OVERLOADING IN CODING (  RHYMES)

4  Example 2: consider the following two methods, public void print(String word) and public void print(String sentence)

5  Example 2: consider the following two methods, public void print(String word) and public void print(String sentence) ^ These are overloaded methods as compiler doesn’t know which method to call. Outcome: nothing will be printed!

6  // volume of a cube int volume(int s) { return(s*s*s); } // volume of a cylinder double volume(double r,int h) { return(3.14*r*r*h); } EXAMPLE 3

7  Excessive overloading may be difficult for developers to understand which overloaded method is being called simply by reading the code. OVERLOADING MAKES IT DIFFICULT TO READ CODE

8 public class AddAmount { public int mathItems(int a, int b) { return a + b; } public int mathItems(int c, int d) { return c+c+d+d; } EXAMPLE 4 – COMPILER CONFUSION

9 ANY QUESTIONS?

10 WORK ON ASSIGNMENT #2


Download ppt "DAY 23 5.2 OVERLOADING.  Overloading: allows for making several methods with the same name (method heading) which differ from each other in the type."

Similar presentations


Ads by Google