• Post Reply Bookmark Topic Watch Topic
  • New Topic

CharSequence Interface  RSS feed

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can somebody explain why the Interface CharSequence contains the toString() method?
========================================================
toString

String toString()
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.
Overrides:
toString in class Object
Returns:
a string consisting of exactly this sequence of characters
========================================================

If I create a Class that implements CharSequence then I don't need to implement the toString() method

If I do override it then surely I am overriding the object.toString() method

So why does CharSequence.toString() exist?

 
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The same reason the Set interface only contains methods that already exist in the Collection interface. These declarations are used so the contract of the method can be further specified (in the JavaDoc).

Object requires toString() to return a non-null value. There are no other restrictions. The CharSequence interface further specifies that the toString() method must return the exact String that the sequence of characters make up.

If I create a Class that implements CharSequence then I don't need to implement the toString() method


Not true. You *must* override this method so you return a String made up from the characters in your CharSequence. If you retain the default toString() implementation, you violate the CharSequence contract.

Another example is Set.equals(). It further specifies the contract of Object.equals() so every Set can be meaningfully compared to every other Set, regardless of their exact implementation.
 
Jon Greenwood
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can create the following Class



I am not explicitly implementing the toString() method

so I assume I am calling Object.toString()
 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jon Greenwood wrote:I can create the following Class


Stephan didn't say that you just can't do it. He said you can't do it without violating the contract. The toString() method of the Object class doesn't return a string that contains any of the characters from the char sequence.

Henry
 
Jon Greenwood
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I am struggling with is that the CharSequence interface violates the statement

A class that implements an interface must implement all the methods declared in the interface.

Summary of Interfaces
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why?

By extending Object (which every class does), you automatically implement toString(). The problem is that the default implementation is not enough to satisfy the contract of CharSequence.
 
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can think of it as an architectural design... Since all objects implicitly inherent from the Object class that has a toString implementation, they will exhibit that default implementation unless overridden... Therefore the CharSequence interface is simply stating that if you want to be my representative, you must override the default toString method and this is how you must do it...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!