简体   繁体   中英

Why does String class extends Object

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 ?

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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM