התיעוד הרשמי של שפת התכנות Java כולל, בין היתר, גם את ה-Java Tutorial אשר חלקים מתוכו זמינים, בין היתר, גם כספרים מודפסים. המדריכים שנכללים ב-Java Tutorials מתעדכנים עם השנים, ורבים מתייחסים אליהם בתור המדריכים הרשמיים של השפה. ההסבר לאופן השימוש ב-super מופיע ב-Java Tutorial שהופק עבור ה-Java Standard Edition.
קריאה להפעלת בנאי ספציפי במחלקה המורישה
באמצעות super ניתן להורות על כך שה-constructor אשר יופעל ב-class המוריש (כחלק משרשרת ה-constructors) אשר פועלים בעת יצירת אובייקט חדש (החלק מה-constructor ב-Object וכלה ב-constructor ב-class שממנו יוצרים אובייקט חדש). להלן ההסבר כפי שהוא מופיע ב-Java Tutorial בקישור https://docs.oracle.com/javase/tutorial/java/IandI/super.html.
Subclass Constructors
The following example illustrates how to use the super
keyword to invoke a superclass's constructor. Recall from the Bicycle
example that MountainBike
is a subclass of Bicycle
. Here is the MountainBike
(subclass) constructor that calls the superclass constructor and then adds initialization code of its own:
public MountainBike(int startHeight, int startCadence, int startSpeed, int startGear) { super(startCadence, startSpeed, startGear); seatHeight = startHeight; }
Invocation of a superclass constructor must be the first line in the subclass constructor.
The syntax for calling a superclass constructor is
super();
or:
super(parameter list);
With super()
, the superclass no-argument constructor is called. With super(parameter list)
, the superclass constructor with a matching parameter list is called.
Note: If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass. If the super class does not have a no-argument constructor, you will get a compile-time error.
Object
does have such a constructor, so if Object
is the only superclass, there is no problem.If a subclass constructor invokes a constructor of its superclass, either explicitly or implicitly, you might think that there will be a whole chain of constructors called, all the way back to the constructor of Object
. In fact, this is the case. It is called constructor chaining, and you need to be aware of it when there is a long line of class descent.
קריאה להפעלת מתודה בגרסה שנדרסה
באמצעות המילה השמורה super ניתן גם לקרוא להפעלת מתודה בגרסה שנדרסה. בין אם מדובר בגרסה שהוגדרה במחלקה המורישה באופן ישיר או שהוגדרה במחלקה המורישה באופן עקיף. כאשר נשתמש במילה השמורה super כדי להפעיל מתודה בגרסה שנדרסה תתבצע בדיקה במעלה ההיררכיה עד אשר תימצא המתודה בגרסה שנדרסה, והיא זו שתופעל. להלן ההסבר כפי שהוא מופיע ב-Java Tutorial בקישור https://docs.oracle.com/javase/tutorial/java/IandI/super.html.
Accessing Superclass Members
If your method overrides one of its superclass's methods, you can invoke the overridden method through the use of the keyword super
. You can also use super
to refer to a hidden field (although hiding fields is discouraged). Consider this class, Superclass
:
public class Superclass { public void printMethod() { System.out.println("Printed in Superclass."); } }
Here is a subclass, called Subclass
, that overrides printMethod()
:
public class Subclass extends Superclass { // overrides printMethod in Superclass public void printMethod() { super.printMethod(); System.out.println("Printed in Subclass"); } public static void main(String[] args) { Subclass s = new Subclass(); s.printMethod(); } }
Within Subclass
, the simple name printMethod()
refers to the one declared in Subclass
, which overrides the one in Superclass
. So, to refer to printMethod()
inherited from Superclass
, Subclass
must use a qualified name, using super
as shown. Compiling and executing Subclass
prints the following:
Printed in Superclass. Printed in Subclass
בדוגמת הקוד הבאה מודגם כיצד מתבצע החיפוש אחר המתודה בגרסה שנדרסה במעלה ההיררכיה עד אשר הגרסה נמצאת.
package com.lifemichael;
public class A {
public void f(int num) {
System.out.println("f inside A");
}
}
package com.lifemichael;
public class B extends A{
public void f(int d) {
System.out.println("f inside B");
}
}
package com.lifemichael;
public class C extends B {
public void f(double num) {
System.out.println("f inside C");
}
}
package com.lifemichael;
public class D extends C {
public void f(int num) {
System.out.println("f inside D");
super.f(num);
}
}
package com.lifemichael;
public class Program {
public static void main(String[] args) {
D ob = new D();
ob.f(123);
}
}
כאשר נריץ דוגמא זו, הפלט שנקבל ידגים את העובדה שכאשר מתוך הפונקציה f הייתה הפעלה של f תוך שליחת של אותו ערך שהגיע (ערך מסוג int) הגרסה של f אשר הופעלה היא הגרסה שמוגדרת ב-B. הסיבה לכך היא שהגרסה שמוגדרת ב-B היא הגרסה שנדרסה על ידי f אשר הוגדרה ב-D ושבדוגמא זו הופעלה. שימו לב לכך שהגרסה שהוגדרה ב-C לא הופעלה. הסיבה לכך היא שמטרת המילה השמורה super היא לאפשר לנו לקרוא להפעלה של המתודה f בגרסה שנדרסה. הגרסה של המתודה ב-C לא נדרסה. ה-type של הפרמטר בגרסה של המתודה f ב-C הוא double.
f inside D
f inside B
כעת נשאלת השאלה מה קורה כאשר אין במעלה ההירארכיה גרסה של המתודה אשר נדרסה. האם נקבל הודעת שגיאה? מבדיקות שביצעתי מסתבר שבמקרה שבו אין במעלה ההירארכיה גרסה של המתודה אשר נדרסה יהיה ניסיון למצוא גרסה שמהווה overloading למתודה שמופעלת ושיכולה לקבל את הערכים הנשלחים (במידה שיש כאלה). הדוגמא להלן זהה לדוגמא הקודמת למעט הבדל אחד. המתודה f אשר מקבלת ערך מסוג int נמחקה גם מ-A וגם מ-B.
package com.lifemichael;
public class A {
//public void f(int num) {
// System.out.println("f inside A");
//}
}
package com.lifemichael;
public class B extends A{
//public void f(int d) {
// System.out.println("f inside B");
//}
}
package com.lifemichael;
public class C extends B {
public void f(double num) {
System.out.println("f inside C");
}
}
package com.lifemichael;
public class D extends C {
public void f(int num) {
System.out.println("f inside D");
super.f(num);
}
}
package com.lifemichael;
public class Program {
public static void main(String[] args) {
D ob = new D();
ob.f(123);
}
}
f inside D
f inside C
אני מקווה שפוסט זה סייע לכם בהבנת אופן הפעולה של המילה השמורה super. ככל שההבנה שתהיה לכם לקוד הנכתב תהיה יותר מדוייקת כך יהיה לכם קל יותר בעת כתיבת הקוד עצמו.