Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I just need hints for this, what I did is I typer public int sum everytime which

ID: 3762700 • Letter: I

Question

I just need hints for this, what I did is I typer public int sum everytime which gava me an error saying (method sum(short,short,int) is already defined in class Overloading..) How should I do it?

Tasked with overloading (creating multiple methods with the same name but different signatures) the sum method. We will consider the following types of inputs: short, int, float, and double. We will overload the sum method for all of these types for two and three parameters. A code skeleton has been provided below showing a method with two parameters. A method with three parameters would have something like int x, short y and double z. You should be implementing about 68 different versions of the sum method.

//This class overloads the sum method several ways. public class OverloadingSum

{

// int public int sum(int x, int y) { return x + y;

}

}

Explanation / Answer

As per what I understood from your question

you got an error because in every definition you used same function name with same datatype and same number of arguments.This has to be avoided.The main logic or concept behind method overloading is to keep the name of the function same,but not the other things like number of arguments or datatypes.

Method overloading :Using same function name but diffrent datatypes or arguements.


There are 2 main ways of performing function or method overloading

Rule1:change the number of arguements or functions your function is taking.

Rule 2:Change the datatypes of method.

Examples for rule 1:

//same function name taking 2 integer arguments

int sum(int x,int y)

or

//same function name taking 1 integer arguments

int sum(int x)

or

//same function name taking 1 integer and 1 float argument

int sum(int x,float y)

or

//same function name taking 3 integer arguments

int sum(int x,inty,in z)

Note:This way you can implement any number of implementations in any permutations by using same function names,but diffrent parameters.But most important thing is that before you define any of these functions,dont forget to declare them at the beginning.All the implementations or function definitions which ever you are using must be declared at the beginning.If you dont do that ,you will get errors ,especially when we deal with function definition which takes same name,same arguments,but they are of diffrent types.

Examples for rule 2:

int sum(int x)

or

float sum(float x)