Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does String class extends Object

Tags:

java

string

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 ?

like image 243
Mohammad Adil Avatar asked Nov 28 '22 15:11

Mohammad Adil


1 Answers

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.

like image 91
adarshr Avatar answered Dec 05 '22 18:12

adarshr