Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

true or false

Posted on 2002-04-16
4
Medium Priority
?
152 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 40 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn different types of Android Layout and some basics of an Android App.
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:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

885 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