Is overloading a method with different access modifier, return type, and parameters still considered overloading?

Viewed 524

This could be just semantics and may be a stupid question, but I'm curious if the following would be considered overloading:

public String name = "name";

public void printName() {
    System.out.println(name);
}

protected String printName(String extra) {
    System.out.println(name + extra);
    return name + extra;
}

Everything I've read says that parameters must be changed for overloading to occur, and that changing access modifiers and return types won't result in a compilation error so long as the parameters are different, but I can't figure out whether the latter would still be considered overloading.

1 Answers

When in doubt, JLS will help:

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.

So it's not "changing the parameters", it is about not override-equivalent. To find out what that is, you go to another chapter, that says:

Two method signatures m1 and m2 are override-equivalent iff either m1 is a subsignature of m2 or m2 is a subsignature of m1.

And the same chapter explains what subsignature is:

The signature of a method m1 is a subsignature of the signature of a method m2 if either:

  • m2 has the same signature as m1, or

  • the signature of m1 is the same as the erasure (§4.6) of the signature of m2.

How you interpret your above methods is an exercise left to you.

Related