wood burning stoves
The moose likes Java in General and the fly likes Relation b/w Object class and Interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Relation b/w Object class and Interface" Watch "Relation b/w Object class and Interface" New topic

Relation b/w Object class and Interface

S Kulkarni

Joined: Jul 09, 2003
Posts: 3
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.
Philip Shanks
Ranch Hand

Joined: Oct 15, 2002
Posts: 189
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!
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
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
Michael Matola
Ranch Hand

Joined: Mar 25, 2001
Posts: 1793
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.
I agree. Here's the link: http://aspose.com/file-tools
subject: Relation b/w Object class and Interface
It's not a secret anymore!