Why is System.out.println (super) not allowed?

Why is System.out.println(super) not allowed?

 System.out.println(this); 

This is normal, and this.toString() is called and printed automatically. Of course, the instance variable is fine, not this .

However, this and super can be used in the same way as I know.

 System.out.println(super); 

So why is this failing? I think it should call super.toString() implicitly. I read the Java specification document, but I did not find a reason.

+6
source share
4 answers

Implementing a standalone super option that violates the sending of a virtual method would be an extremely bad idea.

Think about it for a while.

 abstract class Base { abstract String Description(); String toString() { return "Base"; } } class Derived extends Base { String Description() { return "Derived description"; } String toString() { return "Derived"; } static void use(Base instance) { System.out.println(instance.toString()); System.out.println(instance.Description()); } } 

Now let's take your suggestion and suppose that super really does what you offer; then we can write in Derived :

 class Derived extends Base { // Previous declarations omitted. void useSuper() { Derived.use(super); } void useThis() { Derived.use(this); } static void main() { Derived instance = new Derived(); instance.useThis(); instance.useSuper(); } } 

Now, if I understand you, you assume that the main function should print in order:

  • implementation of toString() from Derived : Derived .
  • implementation of Description() from Derived : "Derived description"
  • implementation of toString() from Base : "Base".
  • implementation of Description() from Base : it does not exist. And two solutions that I can come up with lead to big problems:
    • Raise the exception: congratulations, now you can break any program that relies on abstract methods that are actually implemented without even thinking about it. (How did you know that the function will call the abstract method?)
    • Return implementation from Derived : negotiate sequence.

In short, this use of the word super conceptually violates object-oriented programming.

+4
source

Check the grammar at http://java.sun.com/docs/books/jls/second_edition/html/syntax.doc.html

The keywords super should be followed by SuperSuffix, which cannot be empty.

So, super never stand alone as an expression.

+8
source

this refers to your current object . super belongs to the class super , the class that your current object directly inherits (or may be a super constructor). So

 System.out.println(this) 

outputs your object to the toString () method, but

 System.out.println(super) 

does not work because super is not an object (and therefore does not have a toString () method).

0
source

Super is only relevant for invoking static methods. If you call a non-static method using a super that actually refers to your object, i.e. On this . For example, you can say System.out.println(super.toString()) . This will work and will run toString() real class.

I think this is the reason why passing super as an argument to another method is forbidden.

-5
source

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


All Articles