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

By | July 5, 2019

Question

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?

Solution

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.

Rate this post

Leave a Reply

Your email address will not be published. Required fields are marked *