The super Keyword & protected Members in Inheritance
1. Topic Introduction
When a superclass keeps its fields private (for encapsulation) or when a subclass declares a field/method with the same name as its parent, how can the subclass initialize those hidden parent variables or invoke the parent's version? Java provides the **super keyword—plus the protected modifier** when you intentionally want subclasses across packages to inherit controlled access.
super) & Family-Only Badge (protected)this means 'look inside MY own class'; **super** means 'look one floor up at my immediate parent class!' Whether you want to hand setup values up to your parent's constructor (super(w, h, d)), read a parent variable hidden by a same-named child variable (super.i), or run the parent's original method (super.show()), super reaches directly to your immediate superclass.
super has 2 core uses: (1) super(args) on line 1 of a subclass constructor to invoke the parent constructor; (2) super.member to access hidden parent fields or overridden parent methods.3. Essential Exam Definitions
super Keywordsuper(args)) and access hidden superclass fields or overridden methods (super.member).protected Access Modifier4. Detailed Technical Explanation & Syntax
Two Forms of super (Must Know Both!)
1. **super(arg-list); (Calling Superclass Constructor)**: When a superclass defines private variables initialized by its constructor (e.g., Box(double w, double h, double d)), a subclass BoxWeight calls super(w, h, d); to initialize them without duplicating logic or breaking privacy. Mandatory Rule: super(...) MUST always be the first statement inside the subclass constructor (and cannot coexist with this(...)).
2. **super.member (Accessing Hidden Fields or Overridden Methods)**: Acts like this, except it climbs one level up to the immediate superclass. Solves name hiding when subclass defines int i shadowing superclass int i (super.i vs this.i), and calls parent version of an overridden method (super.show()).
Role of protected Members
private locks out subclasses completely; public exposes fields to the entire world; default exposes members to same-package classes only. **protected** strikes the ideal inheritance middle ground: members remain hidden from unrelated classes in outside packages, yet remain directly accessible to subclasses everywhere!
5. Working Principle: How super(w, h, d) Preserves Encapsulation Across Inheritance
Boxdeclaresprivate double width, height, depth;and constructorBox(w, h, d).BoxWeightdeclaresdouble weight;and callssuper(w, h, d);on line 1.Boxinitializes its own private fields safely;BoxWeightinitializesweight—zero code duplication and zero exposed private fields!
7. Complete Java Code Examples & Tracing
Box / BoxWeight private-field initialization via super(...) and variable shadowing disambiguation (local vs this.i vs super.i).| 1 | class BaseBox { |
| 2 | private double width, height, depth; |
| 3 | protected int tag = 100; // Accessible in subclasses even in other packages |
| 4 | |
| 5 | BaseBox(double w, double h, double d) { |
| 6 | width = w; |
| 7 | height = h; |
| 8 | depth = d; |
| 9 | } |
| 10 | |
| 11 | double volume() { |
| 12 | return width * height * depth; |
| 13 | } |
| 14 | } |
| 15 | |
| 16 | class WeightedBox extends BaseBox { |
| 17 | double weight; |
| 18 | int tag = 200; // Hides BaseBox.tag |
| 19 | |
| 20 | WeightedBox(double w, double h, double d, double m) { |
| 21 | super(w, h, d); // Calls BaseBox constructor (must be line 1) |
| 22 | weight = m; |
| 23 | } |
| 24 | |
| 25 | void showDetails(int tag) { |
| 26 | System.out.println("Local parameter tag: " + tag); |
| 27 | System.out.println("Subclass this.tag: " + this.tag); |
| 28 | System.out.println("Superclass super.tag: " + super.tag); |
| 29 | System.out.println("Volume: " + super.volume() + ", Weight: " + weight); |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | public class SuperKeywordDemo { |
| 34 | public static void main(String[] args) { |
| 35 | WeightedBox wb = new WeightedBox(10, 5, 2, 14.5); |
| 36 | wb.showDetails(300); |
| 37 | } |
| 38 | } |
Local parameter tag: 300 Subclass this.tag: 200 Superclass super.tag: 100 Volume: 100.0, Weight: 14.5
superthisprotectedprivateextends- Placing
weight = m;BEFOREsuper(w, h, d);(compile error: call tosupermust be first statement in constructor). - Writing
super.super.xtrying to skip two levels up (INVALID syntax in Java!). - Using
superinside astaticmethod.
Comparison Tables
this vs super Keyword| Criterion | this | super |
|---|---|---|
| Refers To | Current class instance | Immediate superclass part of current object |
| Constructor Call | this(args) calls overloaded constructor in SAME class | super(args) calls constructor of IMMEDIATE parent class |
| Position Rule | First statement in constructor | First statement in constructor (mutually exclusive with this()) |
| Member Access | Resolves local vs instance variable shadowing (this.x) | Resolves subclass vs superclass shadowing (super.x, super.m()) |
Real-World Applications
- Swing subclasses (
class MyFrame extends JFrame) routinely pass window titles up viasuper("App Title");.
Important KTU Exam Points
- Always include BOTH forms of
super(super(args)andsuper.member) in 6-mark answers. - Highlight the exact three-way scope print (
varvsthis.varvssuper.var).
Memory Techniques & Revision
super= 'Parent Constructor (super()) + Parent Member (super.x)'.
Common Pitfalls to Avoid
- Assuming
super.superexists to access grandparent members directly—Java forbidssuper.super.
KTU-Style Questions & Exam-Ready Answers
Structured specifically for KTU Part A (3-mark) and Part B (6/9-mark) evaluation rubrics.
Introduction: The super keyword allows a subclass to explicitly reference its immediate superclass.
- 1. Invoke Superclass Constructor (
super(arg-list);): Passes initialization values to a superclass constructor (must be line 1 inside the subclass constructor). - 2. Access Superclass Members (
super.member): Reads superclass fields hidden by same-named subclass fields or invokes superclass versions of overridden methods (super.show()).
| class A { int x = 10; A(int v) { x = v; } } |
| class B extends A { |
| int x = 20; |
| B() { super(50); } |
| void print() { System.out.println(this.x + " " + super.x); } |
| } |
Quick Understanding Check
1. Which statement is INVALID in Java?