19 Replies - 4016 Views - Last Post: 08 July 2011 - 12:04 PM
#1
Java method overloading
Posted 08 July 2011 - 10:04 AM
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)
Replies To: Java method overloading
#2
Re: Java method overloading
Posted 08 July 2011 - 10:45 AM
#3
Re: Java method overloading
Posted 08 July 2011 - 10:49 AM
one is (int, int)
the other is (int, double)
so yes they can be both exist and be there....
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
#4
Re: Java method overloading
Posted 08 July 2011 - 10:54 AM
pbl, on 08 July 2011 - 10:49 AM, said:
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
Re: Java method overloading
Posted 08 July 2011 - 11:14 AM
lolitacharm, on 08 July 2011 - 01:04 PM, said:
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:
int numValue(int numOne, int numTwo) and float numValue(int numOne, double numTwo)
Yes they can be both
The only way a method can be
This post has been edited by pbl: 08 July 2011 - 11:27 AM
Reason for edit:: My bad
#6
Re: Java method overloading
Posted 08 July 2011 - 11:14 AM
Quote
-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
Re: Java method overloading
Posted 08 July 2011 - 11:15 AM
pbl, on 08 July 2011 - 07:49 PM, said:
It has everything to do with overloading
Quote
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.
#8
Re: Java method overloading
Posted 08 July 2011 - 11:18 AM
Quote
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
Re: Java method overloading
Posted 08 July 2011 - 11:23 AM
Quote
so they cannot exist
I'll fix back my previous posts
#10
Re: Java method overloading
Posted 08 July 2011 - 11:24 AM
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
Re: Java method overloading
Posted 08 July 2011 - 11:32 AM
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
Re: Java method overloading
Posted 08 July 2011 - 11:34 AM
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
Re: Java method overloading
Posted 08 July 2011 - 11:39 AM
#14
Re: Java method overloading
Posted 08 July 2011 - 11:47 AM
-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
May be because you were answering two threads at the same time
#15
Re: Java method overloading
Posted 08 July 2011 - 11:51 AM
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
|
|

New Topic/Question
Reply



MultiQuote





|