Correct me if I have a wrong train of thought.

First, create an object: new String();

Now we need to declare a variable that will store an object reference: String s = new String();

Now we can use the methods of the created object described in the String class, for example s.charAt(0); .

And if I do it like this Object obj = new String(); then only methods of the class Object will be available to me. Why is that? After all, I will refer to the created instance of the String class through a link?

    2 answers 2

    Because the available members of a class are determined by the type of the reference variable , and not by the type of the object to which it refers.

    The Object superclass is not aware of what the String subclass adds to it.

    At the same time, you can refer to the methods of the subclass by explicitly casting:

     char firstChar = ((String) obj).charAt(0); 

      Mercedes Car class heir. Imagine that string is a Mercedes, and object is a car (car)! Mercedes has the ability to turn the wheels by changing the vertical position. Other machines have no such function. You create a car class without specifying its brand. So the features of this machine are not available to you. You can only steer, press the gas and slow down!