Myślę o tym w ten sposób
+----------------+
| super |
+----------------+ <-----------------+
| +------------+ | |
| | this | | <-+ |
| +------------+ | | |
| | @method1() | | | |
| | @method2() | | | |
| +------------+ | | |
| method4() | | |
| method5() | | |
+----------------+ | |
We instantiate that class, not that one!
Pozwólcie, że przesunę tę podklasę trochę w lewo, aby pokazać, co jest pod spodem ... (Człowieku, uwielbiam grafikę ASCII)
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).
Innymi słowy, cytując ze specyfikacji języka Java :
Formularz super.Identifierodnosi się do pola o nazwie Identifierbieżącego obiektu, ale z bieżącym obiektem wyświetlanym jako instancja nadklasy bieżącej klasy.
Formularz T.super.Identifierodnosi się do pola nazwanego Identifierinstancją obejmującą leksyk, odpowiadającą instancji T, ale z tą instancją postrzeganą jako instancja nadklasy klasy T.
Mówiąc prościej, thisjest w zasadzie obiektem (* obiekt **; ten sam obiekt, po którym można się poruszać w zmiennych), instancją klasy, której dotyczy instancja, zwykłą zmienną w domenie danych; superjest jak wskaźnik do pożyczonego bloku kodu, który chcesz wykonać, bardziej przypomina zwykłe wywołanie funkcji i jest względny w stosunku do klasy, w której jest wywoływany.
Dlatego jeśli używasz superz nadklasy, otrzymujesz kod z wykonanej klasy superduper [dziadek], podczas gdy jeśli używasz this(lub jeśli jest używana niejawnie) z nadklasy, wskazuje ona na podklasę (ponieważ nikt jej nie zmienił - i nikt mógłby).