Skip to main content

super keyword


Definition

  • super is a keyword used to refer to members of the immediate superclass.
  • super cannot be used in static context.

Uses of super

1️⃣ Access Superclass Variables

Syntax:

super.variableName;

Example:

super.a;
super.b;

2️⃣ Access Superclass Methods

Syntax:

super.methodName();

Example:

super.m1();
super.m2();

3️⃣ Invoke Superclass Constructors

Syntax:

super(parameters);

Examples:

super();// Calls default constructor
super(99);// Calls 1-arg constructor
super(99, 88);// Calls 2-arg constructor

Lab440.java

class Lab440 {

public static void main(String[] args) {
Hello h = new Hello();
h.show();
}
}

class Hai {

int a = 10;
}

class Hello extends Hai {

int a = 20;

void show() {
int a = 30;

System.out.println(a); // Local variable
System.out.println(this.a); // Current class variable
System.out.println(super.a); // Parent class variable
}
}

Output:

30
20
10

Lab441.java

class Lab441 {

public static void main(String[] args) {
Hello h = new Hello();
h.show();
}
}

class Hai {}

class Hello extends Hai {

int a = 20;

void show() {
int a = 30;

System.out.println(a); // Local variable
System.out.println(this.a); // Current class variable
System.out.println(super.a); // Compile-time error
}
}

Error:

  • super.aNot allowed
  • Because superclass (Hai) does not have variable a

🧠 Key Concepts

🔹 Variable Priority

Local variable > this.variable > super.variable

🔹 Important Rules

  • super always refers to immediate parent class
  • Works only inside non-static methods / constructors
  • If parent member does not exist → Compilation error

⚡ Quick Summary

  • super.a → access parent variable
  • this.a → access current class variable
  • a → local variable (highest priority)
  • Cannot use super in static context
  • Cannot access non-existing parent members

Lab442.java

class Lab442 {

public static void main(String[] args) {
Hello h = new Hello();
h.show();
}
}

class Hai {

static int a = 10;
}

class Hello extends Hai {

static int a = 20;

void show() {
int a = 30;

System.out.println(a); // local
System.out.println(this.a); // current class static
System.out.println(super.a); // parent static
}
}

Output:

30
20
10

Lab443.java

class Lab443 {

public static void main(String[] args) {
Hello.show();
}
}

class Hai {

static int a = 10;
}

class Hello extends Hai {

static int a = 20;

static void show() {
int a = 30;

System.out.println(a); // ✔ allowed
System.out.println(this.a); // ERROR (no this in static)
System.out.println(super.a); // ERROR (no super in static)
}
}

Key Point:

  • this and super cannot be used in static context

Lab444.java

class Lab444 {

public static void main(String[] args) {
Hello.show();
}
}

class Hai {

static int a = 10;
}

class Hello extends Hai {

static int a = 20;

static void show() {
int a = 30;

System.out.println(a); // local
System.out.println(Hello.a); // current class static
System.out.println(Hai.a); // parent static
}
}

Output:

30
20
10

Lab445.java

class Lab445 {

public static void main(String[] args) {
new B();
}
}

class A {

A(int a) {
System.out.println("A (int) Cons");
}
}

class B extends A {

B() {
super(); // ERROR
System.out.println("B -> D.C.");
}
}

Error:

  • No default constructor in A
  • super() fails because A(int a) exists only

Lab446.java

class Lab446 {

public static void main(String[] args) {
new B();
}
}

class A {

A(int a) {
System.out.println("A (int) Cons");
}
}

class B extends A {

B() {
super(10); // ✔ Correct
System.out.println("B -> D.C.");
}
}

Output:

A (int) Cons
B -> D.C.

Key Concepts from this Page

1. Static Variables with super

  • Can be accessed using:
super.a// from parent
this.a// current class

2. Static Context Rules

Not allowed:

this.a
super.a

✔ Use class name instead:

ClassName.variable

3. Constructor Rules (VERY IMPORTANT)

Rule:

👉 If parent has only parameterized constructor,

child must call it explicitly using super(args)


4. Constructor Execution Flow

Child constructor → calls super(...) → Parent constructor executes first

⚡ Final Summary

  • this & super not allowed in static methods
  • Use ClassName.variable in static context
  • If parent constructor has args → must call super(args)
  • Constructor always executes Parent → Child

Lab447.java

class Lab447 {

public static void main(String[] args) {
new B();
}
}

class A {

A(int a) {
System.out.println("A (int) Cons");
}
}

class B extends A {

B() {
System.out.println("B -> D.C.");
super(10); // ERROR
}
}

Error:

  • super() must be the first statement in constructor
  • Cannot place it after any statement

Lab448.java

class Lab448 {

public static void main(String[] args) {
new B(10);
}
}

class A {

A() {
System.out.println("A -> D.C.");
}
}

class B extends A {

B() {
System.out.println("B -> D.C.");
}

B(int a) {
this(); // calls B()
System.out.println("B (int)");
}
}

Output:

A ->D.C.
B ->D.C.
B (int)

Lab449.java

class Lab449 {

public static void main(String[] args) {
B bobj = new B(10, "JavaWorld");
bobj.show();
}
}

class A {

int a;

A(int a) {
System.out.println("A (int) Cons");
this.a = a;
}
}

class B extends A {

String a;

B(int a1, String a2) {
super(a1); // calls parent constructor
this.a = a2; // assigns child variable
System.out.println("B (int) Cons");
}

void show() {
System.out.println(this.a); // child variable
System.out.println(super.a); // parent variable
}
}

Output:

A (int)Cons
B (int)Cons
JavaWorld
10

Key Concepts from this Page

1. super() Rule (VERY IMPORTANT)

👉 Must be the first statement in constructor

Invalid:

System.out.println("test");
super();// not allowed here

✔ Valid:

super();// must be first
System.out.println("test");

2. this() vs super()

Featurethis()super()
CallsSame class constructorParent class constructor
PositionFirst line onlyFirst line only
Together?Cannot use both in same constructor

3. Constructor Chaining

Flow (Lab448):

B(int) → this() → B() → super() → A()

Execution order:

A → B() → B(int)

4. Variable Shadowing

Lab449 Insight:

  • Same variable name in parent & child → shadowing
  • Access using:
this.a// child
super.a// parent

⚡ Final Summary

  • super() must be first statement
  • this() calls same class constructor
  • Cannot use this() and super() together
  • Constructor execution: Parent → Child
  • Use this & super to resolve variable conflicts