I'm studying the book OCA/OCP Java SE 8 Programmer Practice Tests, by Scott Selikoff and Jeanne Boyarsky. The question 46 at chapter 9 states the following:
46. Which of the following types can you pass as a parameter to the replace() method on the String class?
And the answer is the following:
There are two signatures for the replace() method. One takes two char parameters. The other signature takes a CharSequence. Both String and StringBuilder implement this interface. This makes all three alternatives correct, and Option D is correct.
cleverson teo wrote:However, why String and StringBuider are considered valid, once there isn't a method that takes them as parameter. I couldn't really understand what "Both String and StringBuilder implement this interface" has to do with it.
If classes String and StringBuilder implement interface CharSequence, would these classes not be valid parameters for the second overloaded version of the replace method?
If you check the javadoc of the CharSequence interface, you'll find a nice overview of all known implementing classes.