Java: Calling a super method which calls an overridden method

前端 未结 13 2364
有刺的猬
有刺的猬 2020-11-27 10:29
public class SuperClass
{
    public void method1()
    {
        System.out.println(\"superclass method1\");
        this.method2();
    }

    public void method2(         


        
13条回答
  •  不知归路
    2020-11-27 11:15

    I think of it this way

    +----------------+
    |     super      |
    +----------------+ <-----------------+
    | +------------+ |                   |
    | |    this    | | <-+               |
    | +------------+ |   |               |
    | | @method1() | |   |               |
    | | @method2() | |   |               |
    | +------------+ |   |               |
    |    method4()   |   |               |
    |    method5()   |   |               |
    +----------------+   |               |
        We instantiate that class, not that one!
    

    Let me move that subclass a little to the left to reveal what's beneath... (Man, I do love ASCII graphics)

    We are here
            |
           /  +----------------+
          |   |     super      |
          v   +----------------+
    +------------+             |
    |    this    |             |
    +------------+             |
    | @method1() | method1()   |
    | @method2() | method2()   |
    +------------+ method3()   |
              |    method4()   |
              |    method5()   |
              +----------------+
    
    Then we call the method
    over here...
          |               +----------------+
     _____/               |     super      |
    /                     +----------------+
    |   +------------+    |    bar()       |
    |   |    this    |    |    foo()       |
    |   +------------+    |    method0()   |
    +-> | @method1() |--->|    method1()   | <------------------------------+
        | @method2() | ^  |    method2()   |                                |
        +------------+ |  |    method3()   |                                |
                       |  |    method4()   |                                |
                       |  |    method5()   |                                |
                       |  +----------------+                                |
                       \______________________________________              |
                                                              \             |
                                                              |             |
    ...which calls super, thus calling the super's method1() here, so that that
    method (the overidden one) is executed instead[of the overriding one].
    
    Keep in mind that, in the inheritance hierarchy, since the instantiated
    class is the sub one, for methods called via super.something() everything
    is the same except for one thing (two, actually): "this" means "the only
    this we have" (a pointer to the class we have instantiated, the
    subclass), even when java syntax allows us to omit "this" (most of the
    time); "super", though, is polymorphism-aware and always refers to the
    superclass of the class (instantiated or not) that we're actually
    executing code from ("this" is about objects [and can't be used in a
    static context], super is about classes).
    

    In other words, quoting from the Java Language Specification:

    The form super.Identifier refers to the field named Identifier of the current object, but with the current object viewed as an instance of the superclass of the current class.

    The form T.super.Identifier refers to the field named Identifier of the lexically enclosing instance corresponding to T, but with that instance viewed as an instance of the superclass of T.

    In layman's terms, this is basically an object (*the** object; the very same object you can move around in variables), the instance of the instantiated class, a plain variable in the data domain; super is like a pointer to a borrowed block of code that you want to be executed, more like a mere function call, and it's relative to the class where it is called.

    Therefore if you use super from the superclass you get code from the superduper class [the grandparent] executed), while if you use this (or if it's used implicitly) from a superclass it keeps pointing to the subclass (because nobody has changed it - and nobody could).

提交回复
热议问题