Welcome to the CSC Q&A.
Get help and give help!
Write great code!
It is our choices... that show what we truly are, far more than our abilities.

Categories

+10 votes

Why is substring considered method overloading?

asked in CSC211_Winter2018 by (8 points)

2 Answers

+2 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 (8 points)
+2 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 (4.4k points)
...