• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Relation b/w Object class and Interface

 
S Kulkarni
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am confused by the following statement in the JDK 1.4.2 API documentation regarding the "Charsequence " interface .
The doc mentions that the method toString() in the Charsequence interface OVERRIDES toString in class Object.
1) What is the relation between Object class and Interface ? How come an interface is overriding something in a class ?
2) Does an interface implicitly extend Object just like all other java classes ? In that case why dont the other concerete methods in Object automatically become "public abstract" when some class implements an interface since all methods in an interface are always public abstract ?

thanks in advance.
SK.
 
Philip Shanks
Ranch Hand
Posts: 189
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First off, someone is gonna give you grief for not following the name rules for forum participants... we use real names here.
I was just reading the page for the CharSequence class and I saw that same thing. It's in the 1.4.1 API as well. I think it may be that they added a line like,
public String toString();
to the interface definition, which is pretty pointless. The javadoc engine probably picks that up as overriding the Object toString() method.
Anyway, if you write any interface to specify a public String toString() method, it will automatically be satisfied in the implementing class by inheritance from the Object class.
[ July 09, 2003: Message edited by: Philip Shanks ]
 
Michael Morris
Ranch Hand
Posts: 3451
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the deal here is that there is an implied contract apart from the normal toString() method defined in Object. The docs state:
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.
The problem with this is that there is no way for the compiler to enforce the contract. It's one of those catch 22s, you need toString() to be defined in Object but you also need toString() to have specific behavior for CharSequence implementors.
 
Michael Matola
whippersnapper
Ranch Hand
Posts: 1796
4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Comparator interface has a similar thing going on -- it requires an equals method, which is already implemented in Object.
As far as the interface "overriding" a method -- maybe that's just language that's autogenerated by javadoc and doesn't take into account the fact that it's talking about an interface.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic