• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Interface CharSequence

 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
my question is that Interface CharSequence has a method toString() from the Object class which it overrides. Now Object is a class and not an interface so how can an interface extend a class ???
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
CharSequence doesn't extend Object, but any class that implements CharSequence is going to extend Object. So it's true that to implement toString() in CharSequence, a class will override the version in Object.

The reason CharSequence bothers to include toString(), as opposed to just relying on the toString() defined in Object, is that it is specifying a more specific contract. In Object, toString() just returns an arbitrary string representation. In CharSequence, toString() states that it must return a String containing the same characters as the CharSequence. In effect, the interface contains the method so that it can document this contract.
 
Sheriff
Posts: 22701
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In a similar way, java.util.List and java.util.Set add extra constraints for the equals and hashCode methods. Especially equals is important. For List, it requires that a List is equal to any other List, regardless of the implementing classes, if they have the same elements in the same order. Set has a similar constraint.
 
A wop bop a lu bop a womp bam boom! Tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic