Solved

true or false

Posted on 2002-04-16
4
147 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
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:
Suggested Courses

636 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