day 23 5.2 overloading. overloading: allows for making several methods with the same name (method...

10
DAY 23 5.2 OVERLOADING

Upload: osborn-jenkins

Post on 13-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 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

DAY 23 5.2 OVERLOADING

Page 2: 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

Overloading: allows for making several methods with

the same name (method heading) which diff er 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 diff erent tasks.

OVERLOADING IN CODING (RHYMES)

Page 3: 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

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 fi eld. 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)

Page 4: 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

Example 2: consider the following two methods,

public void print(String word)

and

public void print(String sentence)

Page 5: 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

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!

Page 6: 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

// 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

Page 7: 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

Excessive overloading may be diffi cult for developers

to understand which overloaded method is being

called simply by reading the code.

OVERLOADING MAKES IT DIFFICULT TO READ CODE

Page 8: 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

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

Page 9: 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

ANY QUESTIONS?

Page 10: 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

WORK ON ASSIGNMENT #2