Why String class extends Object

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

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

What is the point of writing an extends Object explicitly where it is not really needed?

is their logical reason for this?

+4
source share
2 answers

This is not in the official oracle JDK source code. Which version are you watching?

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

In any case, it doesn't matter if you wrote extends Object or not in the source code. Beware, however, if you needed to extend another legal class instead of Object , you will run into a dead end since Java does not support multiple inheritance.

Update

The OP is looking for a string declaration in the API documentation. Every class in Java should and should extend Object. However, the other thing is whether the source code says extends Object .

The String class does not have extends Object in the source code. However, an API declaration always indicates that each class implicitly extends an object.

Check out the source code .

+15
source

I assume that you did not install the JDK sources, but look at the file of the decompiled class.

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

+3
source

Source: https://habr.com/ru/post/1499792/


All Articles