Which of these can be used to differentiate two or methods having the same name?

Can two Java methods have the same name with different return type? The return type of the methods are different and they are declared with the same method's name.

Is that allowed?

Which of these can be used to differentiate two or methods having the same name?

Sam R.

15.8k11 gold badges67 silver badges120 bronze badges

asked Apr 6, 2011 at 4:25

7

If both methods have same parameter types, but different return type than it is not possible. From Java Language Specification, Java SE 8 Edition, §8.4.2. Method Signature:

Two methods or constructors, M and N, have the same signature if they have the same name, the same type parameters (if any) (§8.4.4), and, after adapting the formal parameter types of N to the the type parameters of M, the same formal parameter types.

If both methods has different parameter types (so, they have different signature), then it is possible. It is called overloading.

answered Apr 6, 2011 at 4:29

utharkuthark

5,3032 gold badges44 silver badges59 bronze badges

2

Only, if they accept different parameters. If there are no parameters, then you must have different names.

int doSomething(String s);
String doSomething(int); // this is fine


int doSomething(String s);
String doSomething(String s); // this is not

answered Apr 6, 2011 at 4:28

KaivosukeltajaKaivosukeltaja

15.4k4 gold badges40 silver badges70 bronze badges

3

According the JLS, you cannot however due to a feature/bug in the Java 6/7 compiler (Oracle's JDK, OpenJDK, IBM's JDK) you can have different return types for the same method signature if you use generics.

public class Main {
    public static void main(String... args) {
        Main.<Integer>print();
        Main.<Short>print();
        Main.<Byte>print();
        Main.<Void>print();
    }

    public static <T extends Integer> int print() {
        System.out.println("here - Integer");
        return 0;
    }
    public static <T extends Short> short print() {
        System.out.println("here - Short");
        return 0;
    }
    public static <T extends Byte> byte print() {
        System.out.println("here - Byte");
        return 0;
    }
    public static <T extends Void> void print() {
        System.out.println("here - Void");
    }
}

Prints

here - Integer
here - Short
here - Byte
here - Void

For more details, read my article here

answered Apr 6, 2011 at 6:28

Peter LawreyPeter Lawrey

520k75 gold badges740 silver badges1118 bronze badges

2

No. C++ and Java both disallow overloading on a functions's return type. The reason is that overloading on return-type can be confusing (it can be hard for developers to predict which overload will be called). In fact, there are those who argue that any overloading can be confusing in this respect and recommend against it, but even those who favor overloading seem to agree that this particular form is too confusing.

answered Apr 6, 2011 at 4:28

2

You can have two methods with the same arguments and different return types only if one of the methods is inherited and the return types are compatible.

For example:

public class A
{
    Object foo() { return null; }
}

public class B
    extends A
{
    String foo() { return null; }
}

answered Apr 6, 2011 at 5:13

Which of these can be used to differentiate two or methods having the same name?

TofuBeerTofuBeer

60.2k18 gold badges115 silver badges161 bronze badges

1

The java documentation states:

The compiler does not consider return type when differentiating methods, so you cannot declare two methods with the same signature even if they have a different return type.

See: http://docs.oracle.com/javase/tutorial/java/javaOO/methods.html

answered Jul 17, 2014 at 9:01

ChrisChris

4,5651 gold badge32 silver badges37 bronze badges

Even if it is an old thread, maybe some is interested.

If it is an option to you to use the same method inside the same class and archive different return types, use generics: Oracle Lesson Generics

Simple example for generic value holder class:

class GenericValue<T> {
  private T myValue;

  public GenericValue(T myValue) { this.myValue = myValue; }

  public T getVal() { return myValue; }
}

And use it like this:

public class ExampleGenericValue {
  public static void main(String[] args) {
    GenericValue<Integer> intVal = new GenericValue<Integer>(10);
    GenericValue<String> strVal = new GenericValue<String>("go on ...");

    System.out.format("I: %d\nS: %s\n", intVal.getVal(), strVal.getVal());
  }
}

... will result in the following output:

I: 10
S: go on ...

answered Jul 8, 2015 at 12:06

ChristophSChristophS

5684 silver badges23 bronze badges

Only if their parameter declarations are different from memory.

answered Apr 6, 2011 at 4:27

Which of these can be used to differentiate two or methods having the same name?

ChrisChris

7,74510 gold badges64 silver badges96 bronze badges

If it's in the same class with the equal number of parameters with the same types and order, then it is not possible for example:

int methoda(String a,int b) {
        return b;
}
String methoda(String b,int c) {
        return b;    
}

if the number of parameters and their types is same but order is different then it is possible since it results in method overloading. It means if the method signature is same which includes method name with number of parameters and their types and the order they are defined.

Which of these can be used to differentiate two or methods having the same name?

Sam R.

15.8k11 gold badges67 silver badges120 bronze badges

answered Nov 13, 2013 at 18:16

unknownunknown

3705 silver badges17 bronze badges

Can I have two methods with same name?

Two methods may share the same name, provided the number of parameters are different, or if they both have the same parameters, then there is at least one position, i where the parameter types differ.

What is it called when two methods have the same name but different parameters?

Method overloading means two or more methods have the same name but have different parameter lists: either a different number of parameters or different types of parameters.

Which of the following is a method having same name?

Which of the following is a method having same name as that of it's class? Explanation: A constructor is a method that initializes an object immediately upon creation. It has the same name as that of class in which it resides.

Which method has the same name as that of its class *?

Constructor is a special method that is invoked automatically at the time of object creation. It is used to initialize the data members of new objects generally. Constructor in C++ has the same name as class or structure. Hence the correct answer is Constructor.