Solved

true or false

Posted on 2002-04-16
4
143 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Desingning Refactoring existing code 2 31
Is there a simpler dropbox system? 10 44
IBM TS2900 (3572) Tape Autoloader Java? 12 55
Html split(text) 2 26
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
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:

740 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