Closed aquohn closed 5 years ago
I think you will be unable to compile as the method signature is the same.
Oops sorry, I made a typo. They were meant to have different method signatures, with Command for one and ArgCommand for the other.
Does this answer your question: https://stackoverflow.com/questions/36722057/polymorphism-doesnt-work-in-method-arguments-in-java
Alright, so overloading is resolved at compile time by checking the context of the invocation - got it. Thanks!
Here's an issue that seems to be a little obscure on the interwebs: how does the compiler disambiguate which version of an overloaded function to use?
Consider the following simplified example, where ArgCommand is the ancestor for commands which need to take an argument.
Then from the main Duke class we have:
I know passing the string in the first function is superfluous but this is just a simplified example. Anyway, the problem now is, if I call
setupAndRun(someArgCommand)
, will it trigger the first or second function? In general, is the most general or most specific function matched first? Or is it just a matter of which function is written first in the file? Or will this fail to compile completely?