0

What is the real difference between the toString() method in CharSequence interface and toString() method in Object class?

I know that String class by default implements CharSequence and extends Object class.

But does String class give implementation to toString() from CharSequence?If so then Which virsion of toString() gets invoked when we print the String?

azro
  • 53,056
  • 7
  • 34
  • 70
Deepak
  • 19
  • 4
  • Try looking at the source code of `String.toString()`. – Andy Turner Aug 29 '17 at 13:37
  • No ! I just wanna say that we have a toString() given by Object class in our String class! Also we have another toString() via CharSequence(since String implements CharSequence)! Ofcourse String class would have been given an implementation of the abstract method toString() of CharSequence! So what about that when we print String objects?Which one is invoked? And why? because both methods have same prototypes. – Deepak Aug 29 '17 at 13:53

3 Answers3

2

The toString() method is defined in the CharSequence interface, it is not implemented. This is done to add relevant documentation about the requirements that implementations of CharSequence need to follow.

Specifically (Java 8 Update 141), the definition in CharSequence is:

/**
 * Returns a string containing the characters in this sequence in the same
 * order as this sequence.  The length of the string will be the length of
 * this sequence.
 *
 * @return  a string consisting of exactly this sequence of characters
 */
public String toString();

This describes how toString() must behave for CharSequence implementations.

Contrast this with the javadoc in Object:

/**
 * Returns a string representation of the object. In general, the
 * {@code toString} method returns a string that
 * "textually represents" this object. The result should
 * be a concise but informative representation that is easy for a
 * person to read.
 * It is recommended that all subclasses override this method.
 * <p>
 * The {@code toString} method for class {@code Object}
 * returns a string consisting of the name of the class of which the
 * object is an instance, the at-sign character `{@code @}', and
 * the unsigned hexadecimal representation of the hash code of the
 * object. In other words, this method returns a string equal to the
 * value of:
 * <blockquote>
 * <pre>
 * getClass().getName() + '@' + Integer.toHexString(hashCode())
 * </pre></blockquote>
 *
 * @return  a string representation of the object.
 */
public String toString()
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
2

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.

DodgyCodeException
  • 5,963
  • 3
  • 21
  • 42
0

If there are tow methods with the same signature in super class and super interface, the sub class will inherit the one from super class and use it to override the one inherit from super interface.
You can refer to this demo.

    public class Demo {
        public static void main(String args[]) {
            SubClass subClass = new SubClass();
            subClass.printA();  // I inherit from SuperClass
            subClass.printB();  // I inherit from SuperClass
        }
    }


    class SuperClass{
        public void printA(){
            System.out.println("I inherit from SuperClass");
        }

        public void printB(){
            System.out.println("I inherit from SuperClass");
        }
    }

    interface SuperInterface{
        public default void printA(){
            System.out.println("I inherit from SuperInterface");
        };

        void printB();
    }

    class SubClass extends SuperClass implements SuperInterface{
        // No need to override printB() of SuperInterface, 
        // as already inherited one from SuperClass
    }
Leeqihe
  • 21
  • 3