The super keyword serves as an explicit reference to the immediate superclass within a subclass. It enables controlled access to inherited fields, methods, and constructors while enforcing Java's access modifier rules and inheritance chains.
Syntax and Access Rules
- Fields:
super.fieldNameretrieves the value from the superclass. Access is restricted by visibility;privatefields remain inaccessible. - Methods:
super.methodName(arguments)invokes the superclass implementation. Visibility constraints apply identically to fields. - Constructors:
super(argumentList)delegates initialization to a specific superclass constructor. This invocation must occupy the first line of the subclass constructor, appear exactly once, and cannot coexist withthis()in the same constructor block. Static methods cannot utilizesuper.
Code Example
public class BaseSensor {
public int calibrationOffset = 5;
protected double sensitivityFactor = 1.2;
double baselineReading = 0.0;
private String serialCode = "A1B2";
public BaseSensor() {
// Default initialization
}
public BaseSensor(double threshold) {
// Initialization with threshold
}
public void runDiagnostic() {
System.out.println("Running base diagnostic protocol...");
}
protected void adjustCalibration() {
System.out.println("Adjusting calibration in base class...");
}
}
public class AdvancedSensor extends BaseSensor {
public int calibrationOffset = 15; // Shadows parent field
public AdvancedSensor(double threshold) {
// super(threshold); // Calls the parameterized superclass constructor
super(); // Explicit default call
}
public void collectData() {
// Accessing parent fields
System.out.println(super.calibrationOffset + sensitivityFactor + baselineReading);
// super.serialCode; // Compilation error: private member access denied
}
public void performMaintenance() {
super.runDiagnostic();
super.adjustCalibration();
}
public void executeTask() {
// Method resolution order:
// 1. Check current class
// 2. Traverse up the inheritance hierarchy
// Fails at compile time if found but inaccessible, or throws error if absent.
runDiagnostic(); // Option 1: Implicit super call
this.runDiagnostic(); // Option 2: Explicit current instance call
super.runDiagnostic(); // Option 3: Direct superclass bypass
System.out.println(calibrationOffset); // 15 (current class)
System.out.println(this.calibrationOffset); // 15 (current class)
System.out.println(super.calibrationOffset);// 5 (superclass)
}
}
Inheritance Chain Traversal
The super reference is not confined to the direct parent. If an ancestor class several levels up shares a member name with the current class, super resolves to the nearest matching definition in the inheritance hierarchy. When multiple ancestors define identical members, resolution follows a proximity-based search, moving upward from the immediate parent through successive base classes until Object is reached.
Comparison: this vs super
| Aspect | this Keyword |
super Keyword |
|---|---|---|
| Field Access | Resolves within the current class first, then traverses upward. | Bypasses the current class, starting resolution directly in the superclass. |
| Method Invocation | Calls the current class implementation; falls back to parent if absent. | Explicitly invokes the superclass implementation, ignoring overridden versions in the current class. |
| Constructor Delegation | Invokes another constructor within the same class. Must be the first statement. | Invokes a constructor from the direct parent. Must be the first statement. |
| Reference Meaning | Points to the current object instance. | Provides a reference to the parent object portion of the subclass instance. |