I have code like this:
String stringRef = new String("Java"); // (1)
System.out.println("(2): " + stringRef.getClass()); // (2)
System.out.println("(3): " + stringRef.length()); // (3)
Object objRef = stringRef; // (4)
// System.out.println("(5): " + objRef.length()); // (5) Not OK.
System.out.println("(6): " + objRef.equals("Java"));
Why can I not call length() in line (5); and for which class will equals() be called in line (6)?
you cannot do that, as subclass’s methods are not in your super class. In this case, though String is a subclass of Object as length() is not declared in java.lang.Object class its no possible to call length on its instance.
equals() method however is overriden in string class, thus Strings’s equals() will be invoked