3

scenario: There are two methods in same class with same name with different arguments and different access modifiers. E.g.:

public void m1(int a){}
private void m1(String b){}

is it overloading or not?

1
  • 4
    Why do you think it is not? Commented Dec 12, 2017 at 18:02

3 Answers 3

3

Yes - in a word, yes. To quote Oracle's Java tutorial:

This means that methods within a class can have the same name if they have different parameter lists

In other words - the access modifiers are inconsequential to this discussion.

Sign up to request clarification or add additional context in comments.

1 Comment

"signatures that are not override-equivalent" is more specific. public void m1(List<Integer> l) and public void m1(List<String> l) have different parameter lists but are override-equivalent. So no overloading here.
2

Let's refer to the Java Language Specification for this one.

Section 8.4.9 Overloading

If two methods of a class (whether both declared in the same class, or both inherited by a class, or one declared and one inherited) have the same name but signatures that are not override-equivalent, then the method name is said to be overloaded.

From Section 8.4.2, we know that parameter types are part of the signature and your two methods differ in parameter types, so they are overloads.

Section 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.

Comments

1

'Two versions of the same method in the same class' is known as method overloading or compile time polymorphism.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.