super keyword
Definition
superis a keyword used to refer to members of the immediate superclass.supercannot 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.a→ Not allowed- Because superclass (
Hai) does not have variablea
🧠 Key Concepts
🔹 Variable Priority
Local variable > this.variable > super.variable
🔹 Important Rules
superalways 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 variablethis.a→ access current class variablea→ local variable (highest priority)- Cannot use
superin 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:
thisandsupercannot 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 becauseA(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&supernot allowed in static methods- Use
ClassName.variablein 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()
| Feature | this() | super() |
|---|---|---|
| Calls | Same class constructor | Parent class constructor |
| Position | First line only | First 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 statementthis()calls same class constructor- Cannot use
this()andsuper()together - Constructor execution: Parent → Child
- Use
this&superto resolve variable conflicts