Amol Amrutkar
Amol Amrutkar

Reputation: 31

Method overloading or not?

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?

Upvotes: 3

Views: 112

Answers (3)

coder kemp
coder kemp

Reputation: 488

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

Upvotes: 1

Sweeper
Sweeper

Reputation: 271645

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.

Upvotes: 2

Mureinik
Mureinik

Reputation: 311393

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.

Upvotes: 3

Related Questions