Saya memikirkannya seperti ini
+----------------+
| super |
+----------------+ <-----------------+
| +------------+ | |
| | this | | <-+ |
| +------------+ | | |
| | @method1() | | | |
| | @method2() | | | |
| +------------+ | | |
| method4() | | |
| method5() | | |
+----------------+ | |
We instantiate that class, not that one!
Biarkan saya memindahkan subclass itu sedikit ke kiri untuk mengungkapkan apa yang ada di bawah ... (Sobat, saya suka 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).
Dengan kata lain, mengutip dari Spesifikasi Bahasa Jawa :
Formulir super.Identifiermengacu pada bidang bernama Identifierobjek saat ini, tetapi dengan objek saat ini dipandang sebagai turunan dari kelas super kelas saat ini.
Formulir T.super.Identifiermerujuk ke bidang bernama Identifierdari instance yang melingkupi secara leksikal sesuai dengan T, tetapi dengan instance itu dilihat sebagai instance dari superclass T.
Dalam istilah awam, thispada dasarnya adalah sebuah objek (* objek **; objek yang sama yang dapat Anda pindahkan dalam variabel), instance dari kelas yang dipakai, variabel biasa dalam domain data; superseperti penunjuk ke blok kode pinjaman yang ingin Anda jalankan, lebih seperti pemanggilan fungsi belaka, dan ini relatif terhadap kelas tempat pemanggilannya.
Oleh karena itu jika Anda menggunakan superdari superclass Anda mendapatkan kode dari kelas superduper [the grandparent] dieksekusi), sedangkan jika Anda menggunakan this(atau jika digunakan secara implisit) dari superclass itu terus menunjuk ke subclass (karena tidak ada yang mengubahnya - dan tidak ada bisa).