From your question "Which one is invoked?", it sounds as if you think there are two separate toString
methods: one from CharSequence
and one from Object
. That is not the case. In Java, a method with the same name is the same method, regardless of whether it implements a method from one, two or many interfaces.
For instance:
interface I1 {
int foo();
}
interface I2 {
int foo();
}
class C implements I1, I2 {
int foo() {
System.out.println("bar");
}
}
In Java, there is only one method foo()
irrespective of whether it comes via interace I1 or I2. Contrast this with C#, where you can give two different implementations of foo()
: one for each interface.
Looking specifically at your question, when you write a class that implements CharSequence
you are meant to override the toString
method. However, the only thing that makes you do that is the documentation. If you don't override it, you'll inherit Object.toString
. If you do override it, you'll override the one and only one toString
method, since as I've shown above, Object.toString
and CharSequence.toString
are not different.