Praveen
Praveen

Reputation: 121

Overloading methods in Java

class Overload{

  public static void main(String args[]) {
    int[]  number={1,2,3,4,5,6,7,8,9,10};   
    int [] num={1,2,3,4,5};
    int i;
    int sum=0;
    sum = f(number);   
    int sum1= f(num);
    System.out.println("The sum is" +sum + ".");
    System.out.println("The sum is" +sum1 + ".");
  }

  public static int f(int[] value) {
    int i, total = 0;
    for(i=0; i<10; i++) {
      total = total + value[ i ];
    }
    return (total);
  }

  public static int f(int... x) {
    int i, total = 0;
    for(i=0; i<10; i++) {
      total = total + x[ i ];
    }
    return (total);
  }

}

While compiling the above program I'm getting the error as

C:\Program Files\Java\jdk1.7.0_09\bin>javac Overload.java
Overload.java:30: error: cannot declare both f(int...) and f(int[]) in Overload
public static int f(int... x)

Upvotes: 0

Views: 229

Answers (4)

Roman C
Roman C

Reputation: 1

The method signature is the same. Thus overload is impossible. Change the method signature.

There's a definition of method signature in Java from wikipedia.

In the Java programming language, a method signature is the method name and the number and type of its parameters. Return types and thrown exceptions are not considered to be a part of the method signature.

In your code you have two methods with the same signature f(int[] value). Another function uses int... as an argument, but it's equivalent to int[].

Upvotes: 0

JegsVala
JegsVala

Reputation: 1857

Method overloading is a feature of java that allows you to more then one method having the same name but different argument list

METHOD OVERLOADING IS ALSO KNOWN AS STATIC POLYMORPHISM

Argument list must be differ 
 1) Numbers of Parameters "not the same parameters"
 2) Data type of parameters "does not have the same datatype of parameters" 
 3) Sequence of Datatype of parameters "its may be possible to change the sequence the of datatype."

The method overloading have the same argument then it must be the return type is different suppose

 public class Test
 {

   public int printVal(int i,int j)
   {
     System.out.println("Integer Return type")
      return i*j;
   }

   public double printVal(int i,int j)
   {
     System.out.println("Double Return type")
      return i*j;
   }

 }

In the above class we have the two method one have int return type and another have double return type so that can be possible in method overloading.

Sequence of datatype it means

public class Test
{
  public void display(String surname ,String name)
  {
     System.out.println("Surname = "+surname);
     System.out.println("Name    = "+name);

  }

  public void display(String name,String surname)
  {

     System.out.println("Name    = "+name);
     System.out.println("Surname = "+surname);

  }
}

in above example we have two method as a same datatype but its sequence is differ this case is also called method overloading..

Upvotes: 0

PermGenError
PermGenError

Reputation: 46428

your compiler thinks that the method which takes variable arguments as an argument might be the same as an method which takes an array as an argument. i.e., it thinks there are duplicate methods with the same number of arguments, which contrays overloading rules.

public void m1(int[] arr){

}

public void m1(int...i){

}

are basically same.the only difference is var-args can accept any number of int variables

Upvotes: 1

Rohit Jain
Rohit Jain

Reputation: 213361

public static int f(int... x)

is nothing but: -

public static int f(int[] x)

with only difference that it does not necessarily needs an argument to be passed. And when you pass individual elements, they are converted to an array internally. So, you are actually passing an array only.

Whereas the later one needs an argument. An empty array at the least.

And both the methods are eligible to be invoked, if you are passing an array as argument.

So the call:

f(new int[] {1, 2});

can be made to both the methods. So, an ambiguity is there.


However, f(5) call can only be made for the first method. Since, 5 cannot be assigned to an array type. So, the ambiguity only occurs when you are passing an array.

Upvotes: 4

Related Questions