• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

CharSequence Interface

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
reply
    Bookmark Topic Watch Topic
  • New Topic