2

I'm a High school student, I studied a little bit of Java by myself. My teacher asked if you can write two methods with switched but same parameters. For example:

public void method (String arg1, int arg2){
}

public void method(int arg1, String arg2){
}

I said yes, the teacher said that I don't know what overloading means, but I tested and it worked, and then she said "JDK has a bug" and she got mad at me.

I need a super and complete answer.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • Does this answer your question? [Polymorphism vs Overriding vs Overloading](https://stackoverflow.com/questions/154577/polymorphism-vs-overriding-vs-overloading) – Julien Feb 18 '20 at 13:36
  • 2
    As long as the parameters are **not of the same type**, you can switch them to change the method signature... If both of them are `String`s, for example, you cannot... – deHaar Feb 18 '20 at 13:36
  • Maybe look at https://docs.oracle.com/javase/tutorial/java/javaOO/methods.html. – Ralf Renz Feb 18 '20 at 13:37
  • 1
    First rule of power: https://fs.blog/2012/11/never-outshine-the-master/ never outshine the master. – Gaetano Piazzolla Feb 18 '20 at 13:57
  • Thank you all for the answer. What can i do with my teacher? She thinks im acting superior but im not. She just said that im "stupid" just only because im right.. – Andrea D'Adamo Feb 18 '20 at 15:13
  • This would [not be the first time a teacher is wrong](https://stackoverflow.com/questions/52264638/is-main-a-valid-java-identifier). – MC Emperor Feb 18 '20 at 15:15
  • 1
    @AndreaD'Adamo Maybe [this post here on another StackExchange site](https://interpersonal.stackexchange.com/questions/2405/how-to-politely-correct-a-teacher) will help... – MC Emperor Feb 18 '20 at 15:21
  • @MC Emperor Ty a lot, i will probably say sorry about acting 'rude' and explain why i think she Is wrong giving some answers.(Sorry for bad english) – Andrea D'Adamo Feb 18 '20 at 15:28

3 Answers3

1

As mentioned in the official Oracle Java tutorial:

Java can distinguish between methods with different method signatures

On the same page, "method signature" is defined as

Two of the components of a method declaration comprise the method signature—the method's name and the parameter types.

Since the parameter types are a list, they also have a fixed order (Unlike e.g. a set).

Thus two parameter lists with the same types (But a different order) are considered different parameter type lists, which in turn allows you to declare two methods with the same name and these two parameter type lists without causing compile time errors.

Edit: For more details refer to the Java Language Specification, chapter 8.4.2.

Nightara
  • 117
  • 5
0

Overloading allows different methods to have the same name, but different signatures where the signature can differ by the number of input parameters, or type of input parameters, or order of input parameters.

Uladzislau Kaminski
  • 2,113
  • 2
  • 14
  • 33
0

If you are talking about methods on same class, it's possible in Java and is legit:

public class Test {

    public static void main(String[] args) {
        Test test = new Test();

    }

    public void method (String arg1, int arg2){
    }

    public void method(int arg1, String arg2){
    }
}

This works because two methods have same name, but differnt type for parameters, so for Java there are two different methods.

Vokail
  • 630
  • 8
  • 27