Solved

true or false

Posted on 2002-04-16
4
142 Views
Last Modified: 2010-03-31
hi i have this sentence talking about overloading.. i need to know if it's right or wrong and why

Method that overload a name are different methods and can coexist in a single class.


so waiting for the answer and why?

0
Comment
Question by:ms_lost
4 Comments
 
LVL 92

Expert Comment

by:objects
ID: 6946062
tru, a class can have methods with the same name, but different arguments.

eg.
int f(int a);
int f(int a, int b);
0
 

Author Comment

by:ms_lost
ID: 6946088
hope it's right..
actually i found this question in some inhereted papers lol..and the instructor marked it wrong.. where the student put True.. and it was corrected as not different methods but same methods..
that's why i was confused..
0
 

Accepted Solution

by:
jodear earned 10 total points
ID: 6947057
I agree with objects.  The instructor was in err technically because the methods are declared differently and may contain different statements.

The answer to your question, "Why?"
Ans:  Naming methods is to help the programmer understand what the method does.  If two different methods does the same thing (let's say both methods adds 2 numbers), the only difference is the type of their arguments (let's say one method adds 2 integers while the other method adds 2 floats), then it would be meaningful for the programmer to create both methods with the same name (let's say, 'add').

Illustration:

private int add(int x, int y){
   return (x+y);
}
private float add(float x, float y){
   return (x+y);
}

When you use the method 'add' with 2 integers or 2 floats as parameters, it would still work fine.

Illustration:

System.out.println(add(1,2));
System.out.println(add(1.5,0.3));

The first one gives you an integer result, the other one gives you a float result.
0
 
LVL 7

Expert Comment

by:tomboshell
ID: 6947064
I've seen it listed as both.  Overloading a method.  If I am getting the correct term, it is when the class contains many declarations of the same method with different parameters (and possibly different functionality but it should really have the same functionality all across.)  You will always call 'f' but you will give it different parameters.  So, it is the same.  The class will look for an 'f' that accepts the parameters that are given.  The resolution will be carried out at runtime.  
0

Featured Post

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question