Welcome to the CSC Q&A, on our server named in honor of Ada Lovelace. Write great code! Get help and give help!
It is our choices... that show what we truly are, far more than our abilities.

Categories

+14 votes

Why is substring considered method overloading?

asked in CSC211_Winter2018 by (1 point)

3 Answers

+3 votes

Because substring can be used in 2 ways. substring(index1, index2) will grab characters from index1 to index2 while substring(index1) will grab from index1 to the end of the line.

Method overloading just means a method can go different things given different arguments.

answered by (1 point)
+3 votes

You can also see an answer to a previous question about the Math class and method overloading.

Methods are overloaded if there are two methods in the SAME class with the same name, but different signatures. Different signatures means different parameters lists in the number of parameters, the types or both.

The headers for the two substring methods in the String class are:
public String substring(int beginIndex) and public String substring(int beginIndex, int endIndex) which obviously differ in the number of parameters.

See my previous answer about the math class which has overloaded methods because the parameters differ in type.

answered by (1 point)
0 votes

It's if they have the methods have the same name but different signatures.

Different signatures meaning different parameters or the number of parameters being different.

answered by (1 point)
...