Java method overloading

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 5783 Views - Last Post: 08 July 2011 - 12:04 PM Rate Topic: -----

#1 lolitacharm  Icon User is offline

  • D.I.C Head

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

Java method overloading

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
  • +

Replies To: Java method overloading

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2118
  • View blog
  • Posts: 3,244
  • Joined: 21-June 11

Re: Java method overloading

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.
Was This Post Helpful? 2
  • +
  • -

#3 pbl  Icon User is offline

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

Reputation: 8334
  • View blog
  • Posts: 31,858
  • Joined: 06-March 08

Re: Java method overloading

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
Reason for edit:: My bad

Was This Post Helpful? 1
  • +
  • -

#4 lolitacharm  Icon User is offline

  • D.I.C Head

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

Re: Java method overloading

Posted 08 July 2011 - 10:54 AM

View Postpbl, 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?
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

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

Reputation: 8334
  • View blog
  • Posts: 31,858
  • Joined: 06-March 08

Re: Java method overloading

Posted 08 July 2011 - 11:14 AM

View Postlolitacharm, 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.

View Postlolitacharm, 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
Reason for edit:: My bad

Was This Post Helpful? 1
  • +
  • -

#6 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Java method overloading

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 :)
Was This Post Helpful? 3
  • +
  • -

#7 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2118
  • View blog
  • Posts: 3,244
  • Joined: 21-June 11

Re: Java method overloading

Posted 08 July 2011 - 11:15 AM

View Postpbl, 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 :)

It has everything to do with overloading

Quote

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

You're confusing overriding and overloading.

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.
Was This Post Helpful? 3
  • +
  • -

#8 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Java method overloading

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

Was This Post Helpful? 1
  • +
  • -

#9 pbl  Icon User is offline

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

Reputation: 8334
  • View blog
  • Posts: 31,858
  • Joined: 06-March 08

Re: Java method overloading

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
Was This Post Helpful? 0
  • +
  • -

#10 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7807
  • View blog
  • Posts: 13,201
  • Joined: 19-March 11

Re: Java method overloading

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.
Was This Post Helpful? 1
  • +
  • -

#11 lolitacharm  Icon User is offline

  • D.I.C Head

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

Re: Java method overloading

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?
Was This Post Helpful? 0
  • +
  • -

#12 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7807
  • View blog
  • Posts: 13,201
  • Joined: 19-March 11

Re: Java method overloading

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

Was This Post Helpful? 1
  • +
  • -

#13 lolitacharm  Icon User is offline

  • D.I.C Head

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

Re: Java method overloading

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?
Was This Post Helpful? 0
  • +
  • -

#14 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Java method overloading

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

May be because you were answering two threads at the same time :) I see you are answering this overriding thread: http://www.dreaminco...__fromsearch__1
Was This Post Helpful? 1
  • +
  • -

#15 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7807
  • View blog
  • Posts: 13,201
  • Joined: 19-March 11

Re: Java method overloading

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

/Users/jon/java/junk:538 $ java OverLoad
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

Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2