Is it possible to have two methods in a class with same method signature but different return types?

September 26, 2020Guidelines

Contents

  • 1 Can we have same method name with different return type?
  • 2 What if 2 functions in a class have same name and input parameters but different return types will it work?
  • 3 What is it called when more than one function has the same name?
  • 4 When do method names need to be the same?
  • 5 Why are method names and parameters different in Java?
  • 6 Can a method have the same signature as another method?

Can we have same method name with different return type?

You can not define more than one method with the same name, Order and the type of the arguments. The compiler does not consider the return type while differentiating the overloaded method. But you cannot declare two methods with the same signature and different return type. It will throw a compile-time error.

What if 2 functions in a class have same name and input parameters but different return types will it work?

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.

Can different classes have the same method name?

Yes, It is allowed to define a method with the same name as that of a class. There is no compile-time or runtime error will occur. Normally the constructor name and class name always the same in Java.

What is it called when more than one function has the same name?

Definition: Two or more functions can have the same name but different parameters; such functions are called function overloading. C++ has many features, and one of the most important features is function overloading. It is a code with more than one function with the same name having various types of argument lists.

When do method names need to be the same?

@Divine – When overloading the method names must be the same, but the parameter lists must be different. They can differ in the types and/or the number of parameters. The don’t need to have the same return type, it’s just that overload resolution is done only on the parameters of a method, not the return type.

Can two methods have the same name with different return types?

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

Why are method names and parameters different in Java?

The combination of the method name and the parameter list. This happens because it is not possible for the jvm two choose between to methods that differs only from the return type. The jvm infact can investigate only the name of the method and the type of parameters of the call. So your example is not possible in java.

Can a method have the same signature as another method?

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.

This chapter reviews method parameters and local variables, as well as method overloading and method signature.

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. When a method is called, the corresponding method is invoked by matching the arguments in the call to the parameter lists of the methods. The name together with the number and types of a method's parameter list is called the signature of a method. The return type itself is not part of the signature of a method.

18. Return Type is Not Part of the Signature

Answer:

No.

The names of the formal parameters are not part of the signature, nor is the return type. The signatures of the two methods are:

    chargePenalty( int ) chargePenalty( int )

Both methods have the same signature.

Return Type is Not Part of the Signature

It might seem strange that the return type is not part of the signature. But a potentially confusing situation is avoided by keeping the return type out of the signature. Say that there were two methods that differ only in their return type:

float chargePenalty( int amount  ) { ... }
int   chargePenalty( int penalty ) { ... }

and that main used one of the methods:

class CheckingAccountTester
{
public static void main( String[] args )
{
CheckingAccount bobsAccount = new CheckingAccount( "999", "Bob", 100 );

double result = bobsAccount.chargePenalty( 60 );

}
}

Which method should be called?

Either method matches the statement, since both have and int parameter and both return a type that can be converted to double. (Both int and float can be converted to double.)

To avoid confusion in situations like this, the return type is not counted as part of the signature.


Question 18:

Say that a class has these two methods:

    public void changeInterestRate( double newRate ) { ... } public void changeInterestRate( int newRate ) { ... }

Do these methods have unique signatures?

Can you have two methods in a class with the same method signature but different return types yes?

The compiler does not consider the return type while differentiating the overloaded method. But you cannot declare two methods with the same signature and different return types. It will throw a compile-time error. If both methods have the same parameter types, but different return types, then it is not possible.

Can we have same method name with different return type?

No, you cannot overload a method based on different return type but same argument type and number in java. same name.

Can a class have two methods with the same name?

Yes, we can define multiple methods in a class with the same name but with different types of parameters.

Can two methods of a class have the same name and the same signature?

Example 2: Class with Two Methods with the Same Signature In java, we cannot implement two methods of the same signature i.e of the same method name, same parameter types, and the same return type.