• (2 Pages)
• 1
• 2

## 19 Replies - 13090 Views - Last Post: 08 July 2011 - 12:04 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=238630&amp;s=74f1c4c3d625d2b55a03c09cfe0144a4&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 lolitacharm

Reputation: 2
• Posts: 118
• Joined: 25-June 11

Posted 08 July 2011 - 10:04 AM

Can two methods be overloaded even if they have the exact same parameter. I know that the return type does not matter, even if it's different, but for example, can these be overloaded, even though they both have int numOne??

int numValue(int numOne, int numTwo) and float numValue(int numOne, double numTwo)

int numValue(int numOne, int numTwo) and int numValue(int numOne, double numTwo)
Is This A Good Question/Topic? 0

### #2 sepp2k

• D.I.C Lover

Reputation: 2616
• Posts: 4,173
• Joined: 21-June 11

Posted 08 July 2011 - 10:45 AM

int numValue(int numOne, int numTwo) and float numValue(int numOne, double numTwo) differ in the type of the second parameter (int vs. double), so yes, you can define both. The fact that the first parameter is the same doesn't matter. As long as at least one parameter has a different type (or the number of parameters differs), it's a valid overload.

### #3 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

Posted 08 July 2011 - 10:49 AM

They don't have the same signature:
one is (int, int)
the other is (int, double)

so yes they can be both exist and be there.... but this has nothing to do with overloading

A subclass will overload its father method only if they have the same signature

This post has been edited by pbl: 08 July 2011 - 11:24 AM

### #4 lolitacharm

Reputation: 2
• Posts: 118
• Joined: 25-June 11

Posted 08 July 2011 - 10:54 AM

pbl, on 08 July 2011 - 10:49 AM, said:

They don't have the same signature:
one is (int, int)
the other is (int, double)

so yes they can be both exist and be there.... but this has nothing to do with overloading

A subclass will overload its father method only if they have the same signature

What do you mean this had nothing to do with overloading? I guess my question then is, can numValue be overloaded as those two examples, in each separate case that is?

### #5 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

Posted 08 July 2011 - 11:14 AM

lolitacharm, on 08 July 2011 - 01:04 PM, said:

Can two methods be overloaded even if they have the exact same parameter.

You cannot have two methods in the same class with the same name and the same signature so your code won't compile if they have the exact same parameter. So your question does not really make sense.

lolitacharm, on 08 July 2011 - 01:04 PM, said:

I know that the return type does not matter, even if it's different, but for example, can these be overloaded, even though they both have int numOne??

int numValue(int numOne, int numTwo) and float numValue(int numOne, double numTwo)

Yes they can be both overload overide by their sons.

The only way a method can be overloaded overriden it is because one of its subclass have a method with the same name and exactly the same parameters. If the parameters are different it can be overloaded.

This post has been edited by pbl: 08 July 2011 - 11:27 AM

### #6 smohd

• Critical Section

Reputation: 1824
• Posts: 4,627
• Joined: 14-March 10

Posted 08 July 2011 - 11:14 AM

I can say:

Quote

Overloading lets you make multiple versions of a method, with different argument lists, for convenience too the callers...
-You can change the return type as long as the argument lists are different(which means, if only the return type is different, it is not a valid overload- the compiler will assume you are trying to override the method)
-You can vary the access level in any direction.

NOTE: An overloaded method is not the same as an overridden method

### #7 sepp2k

• D.I.C Lover

Reputation: 2616
• Posts: 4,173
• Joined: 21-June 11

Posted 08 July 2011 - 11:15 AM

pbl, on 08 July 2011 - 07:49 PM, said:

so yes they can be both exist and be there.... but this has nothing to do with overloading

Quote

A subclass will overload its father method only if they have the same signature

If you have two methods with the same name, but different signatures, that's overloading. If a subclass defines a method with the same name and signature as a superclass, that's overriding.

### #8 smohd

• Critical Section

Reputation: 1824
• Posts: 4,627
• Joined: 14-March 10

Posted 08 July 2011 - 11:18 AM

Quote

The only way a method can be overloaded it is because one of its subclass have a method with the same name and exactly the same parameters. If the parameters are different it can be overloaded.

Sorry, pbl I have a question here: Is this overloading or overriding here? Sorry

This post has been edited by smohd: 08 July 2011 - 11:19 AM

### #9 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

Posted 08 July 2011 - 11:23 AM

Sorry my bad,... guess that the inital question mixed me up

Quote

Can two methods be overloaded even if they have the exact same parameter

so they cannot exist

I'll fix back my previous posts

### #10 jon.kiparsky

• Beginner

Reputation: 11067
• Posts: 18,903
• Joined: 19-March 11

Posted 08 July 2011 - 11:24 AM

overloading is defining multiple methods with different signatures. Overriding is redefining a method inherited from a superclass.

```public void toString(){}
```

is always overriding (inherited from Object)

```public void toString(boolean parameter) {}
```

is overloading - presumably the parameter tells us something about what sort of representation we want, or something.

### #11 lolitacharm

Reputation: 2
• Posts: 118
• Joined: 25-June 11

Posted 08 July 2011 - 11:32 AM

Okay, sorry if I am causing confusion, I am not talking about overriding at all. ONLY overloading. So. My question is then:

1. can numValue be overloaded as:

int numValue(int numOne, int numTwo) and float numValue(int numOne, double numTwo)

??

2. can numValue be overloaded as:
int numValue(int numOne, int numTwo) and int numValue(int numOne, double numTwo)

??

does that make more sense?

### #12 jon.kiparsky

• Beginner

Reputation: 11067
• Posts: 18,903
• Joined: 19-March 11

Posted 08 July 2011 - 11:34 AM

yes in both cases. Both times you have two completely different methods, which to the compiler are completely unrelated.

you could also have
```int numValue (double numOne, int numTwo){}
```

and
```int numValue(double numOne, double numTwo){}
```

All four are okay in the same class.

This post has been edited by jon.kiparsky: 08 July 2011 - 11:36 AM

### #13 lolitacharm

Reputation: 2
• Posts: 118
• Joined: 25-June 11

Posted 08 July 2011 - 11:39 AM

but for the second one, can't it NOT be overloaded, because what if two ints are entered? Couldn't either be used because the second int could be automatically cast to type double?

### #14 smohd

• Critical Section

Reputation: 1824
• Posts: 4,627
• Joined: 14-March 10

Posted 08 July 2011 - 11:47 AM

To add from what jon.kiparsky said, my previous post showed you rules to care for a valid overloading method:
-The name should be the same, but different parameters
-The return type can change...
-But you cant change only the return type...

So, the first one you change the return type and paramets, it is valid
and the second one you change only parameter list, its also valid

Quote

Sorry my bad,... guess that the inital question mixed me up

### #15 jon.kiparsky

• Beginner

Reputation: 11067
• Posts: 18,903
• Joined: 19-March 11

Posted 08 July 2011 - 11:51 AM

Take a look at this:

```public class OverLoad{

public static void method(double x, int y)
{
System.out.println("called methodOne, double int");
}
public static void method(double x, double y)
{
System.out.println("called methodTwo double double");
}
public static void method(int x, double y)
{
System.out.println("called methodThree, int double");
}
public static void method(int x, int y)
{	System.out.println("called methodFour, int int");
}

public static void main (String[] args)
{
method(3.0d, 1);
method(3.0d, 3.0d);
method(1, 3.0d);
method(1, 1);
method(3.0f, 1);
}
}
```

Output is

called methodOne, double int
called methodTwo double double
called methodThree, int double
called methodFour, int int
called methodOne, double double

Notice that the last call, it promotes the float to a double.

Now, what do you think will happen if we remove the (int, int) version of the method, but keep the (int, int) call?

How would you find out if your guess is correct?

This post has been edited by jon.kiparsky: 08 July 2011 - 11:54 AM