Solved

true or false

Posted on 2002-04-16
4
141 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

771 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now