KTU OOP Java2024 Scheme • 60M ESE
MODULE 2/Section 2: Inheritance

The super Keyword & protected Members in Inheritance

HIGH PRIORITY (Prep Guide)Official Syllabus~13 min study
Official KTU 2024 Syllabus Topics Covered Here
The super keywordAccessing superclass membersProtected members

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.

2. Start From Zero: Calling Your Parent Department (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.

Key Takeaway: 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

The super Keyword
A reference keyword inside a subclass pointing to its immediate superclass, used to invoke superclass constructors (super(args)) and access hidden superclass fields or overridden methods (super.member).
protected Access Modifier
Visibility level granting access to the declaring class, all classes in the same package, AND all subclasses—even if those subclasses live in DIFFERENT packages.

4. 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

  1. Box declares private double width, height, depth; and constructor Box(w, h, d).
  2. BoxWeight declares double weight; and calls super(w, h, d); on line 1.
  3. Box initializes its own private fields safely; BoxWeight initializes weight—zero code duplication and zero exposed private fields!

7. Complete Java Code Examples & Tracing

Using `super(args)` for Private Fields & `super.member` for ShadowingSuperKeywordDemo.java
Combines Box / BoxWeight private-field initialization via super(...) and variable shadowing disambiguation (local vs this.i vs super.i).
1class 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
16class 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
33public 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}
Expected Output
Local parameter tag: 300
Subclass this.tag: 200
Superclass super.tag: 100
Volume: 100.0, Weight: 14.5
Keywords:superthisprotectedprivateextends
Common Exam & Coding Mistakes
  • Placing weight = m; BEFORE super(w, h, d); (compile error: call to super must be first statement in constructor).
  • Writing super.super.x trying to skip two levels up (INVALID syntax in Java!).
  • Using super inside a static method.

Comparison Tables

this vs super Keyword
Criterionthissuper
Refers ToCurrent class instanceImmediate superclass part of current object
Constructor Callthis(args) calls overloaded constructor in SAME classsuper(args) calls constructor of IMMEDIATE parent class
Position RuleFirst statement in constructorFirst statement in constructor (mutually exclusive with this())
Member AccessResolves 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 via super("App Title");.

Important KTU Exam Points

  • Always include BOTH forms of super (super(args) and super.member) in 6-mark answers.
  • Highlight the exact three-way scope print (var vs this.var vs super.var).

Memory Techniques & Revision

  • super = 'Parent Constructor (super()) + Parent Member (super.x)'.

Common Pitfalls to Avoid

  • Assuming super.super exists to access grandparent members directly—Java forbids super.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?