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 ?
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 ]
Philip Shanks, SCJP - Castro Valley, CA
My boss never outsources or has lay-offs, and He's always hiring. I work for Jesus! Prepare your resume!
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.
Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
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.