6

Looking at the String class declaration , you can see that it extends Object class.

public final class String
extends Object
implements Serializable, Comparable<String>, CharSequence

What is the point of explicitly writing extends Object where it actually isn't required ?

is their any logical reason behind this ?

1
  • 14
    It's ancient Java history, go to an archaeological site to ask :) Commented Aug 30, 2013 at 12:42

2 Answers 2

24

It doesn't in the official oracle JDK source code. Which version are you looking at?

public final class String
    implements java.io.Serializable, Comparable<String>, CharSequence

Either way, it wouldn't make a difference whether you wrote extends Object or not in the source code. Beware though, if you had to extend another legitimate class instead of Object, you'll hit a dead end as Java doesn't support multiple inheritence.

Update

The OP is looking at the declaration of String in the API documentation. Every class in Java must and do extend Object. However, it is a different thing whether the source code says extends Object.

The String class doesn't have extends Object in the source code. However, the API declaration always indicates that every class extends Object implicitly.

Have a look at the source code.

Sign up to request clarification or add additional context in comments.

3 Comments

Well, that's not the source code. Every class in Java implicitly extends Object. Nobody normally explicitly declares it as extends Object.
Ahh.. I completely missed that i was looking at docs and not at the actual source.
Just rephrase your question using Character as an example and it is correct. However, in OpenJDK 7 they have removed the extends Object designation.
3

My guess is you don't have installed the JDK sources but are looking at the decompiled class file.

In my source folder, the extends Object is missing. The decompiler would just always add extends <superclass>. But since in Java every class is by definition derived from Object, this will become extends Object in the case of the String class.

3 Comments

How does that answer the question?
@loki To be fair, this could have turned out to be the correct explanation. The actual source code of String does not declare "extends Object".
@MarkoTopolnik: Agree. Read question few more times to exactly understand it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.