The super keyword helps you access methods and variables from a parent class. It is useful when you want to use or extend the behavior of the parent class.
Super keyword in Java
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Java
super.methodName(); super.variableName; super(); // calls parent class constructor
super() must be the first statement in a child class constructor if used.
You can use super to avoid confusion when child and parent have variables or methods with the same name.
Examples
super calls the parent method and accesses the parent variable.Java
class Parent { int x = 10; void show() { System.out.println("Parent show"); } } class Child extends Parent { int x = 20; void show() { System.out.println("Child show"); super.show(); // calls Parent's show System.out.println(super.x); // accesses Parent's x } }
super() calls the parent constructor from the child constructor.Java
class Parent { Parent() { System.out.println("Parent constructor"); } } class Child extends Parent { Child() { super(); // calls Parent constructor System.out.println("Child constructor"); } }
Sample Program
This program shows how the child class uses super to access the parent class variable and method even when they are overridden or hidden.
Java
class Parent { int number = 100; void display() { System.out.println("Parent display: " + number); } } class Child extends Parent { int number = 200; void display() { System.out.println("Child display: " + number); System.out.println("Parent number using super: " + super.number); super.display(); } } public class Main { public static void main(String[] args) { Child c = new Child(); c.display(); } }
Important Notes
If you do not use super() in a child constructor, Java automatically calls the parent's no-argument constructor.
You cannot use super in static methods because super refers to instance members.
Summary
super helps access parent class methods and variables.
Use super() to call the parent constructor from a child constructor.
It is useful when child class overrides or hides parent class members.
Practice
1. What does the
super keyword do in Java?easy
Solution
Step 1: Understand the role of
Thesupersuperkeyword is used to refer to the parent class's members (methods or variables) from a child class.Step 2: Compare options with definition
Only It accesses methods and variables from the parent class. correctly describes this behavior. Other options describe unrelated actions.Final Answer:
It accesses methods and variables from the parent class. -> Option CQuick Check:
super accesses parent members = A [OK]
Hint: Remember: super means parent class access [OK]
Common Mistakes:
- Thinking super creates new objects
- Confusing super with this keyword
- Assuming super ends program
2. Which of the following is the correct way to call a parent class constructor in Java?
easy
Solution
Step 1: Recall syntax for parent constructor call
In Java,super()is used inside a child constructor to call the parent class constructor.Step 2: Evaluate options
Only super(); uses the correct keywordsuper(). Others are invalid or refer to different concepts.Final Answer:
super(); -> Option AQuick Check:
Parent constructor call = super() [OK]
Hint: Use super() to call parent constructor [OK]
Common Mistakes:
- Using this() instead of super()
- Trying to call parent() which is invalid
- Confusing base() with super()
3. What will be the output of the following code?
class Parent {
int x = 10;
}
class Child extends Parent {
int x = 20;
void printX() {
System.out.println(super.x);
}
}
public class Test {
public static void main(String[] args) {
Child c = new Child();
c.printX();
}
}medium
Solution
Step 1: Understand variable hiding and super usage
The child class has its ownx = 20, butsuper.xaccesses the parent'sxwhich is 10.Step 2: Trace the print statement
The methodprintX()printssuper.x, so it prints 10.Final Answer:
10 -> Option DQuick Check:
super.x accesses parent variable = 10 [OK]
Hint: super.variable accesses parent class variable [OK]
Common Mistakes:
- Printing child variable instead of parent
- Confusing super.x with this.x
- Expecting compilation error
4. Identify the error in this code snippet:
class Parent {
void show() {
System.out.println("Parent show");
}
}
class Child extends Parent {
void show() {
super.show();
System.out.println("Child show");
}
void display() {
super();
}
}medium
Solution
Step 1: Check usage of super in methods
Callingsuper.show()inside overridden method is valid to call parent method.Step 2: Analyze
super()call in display()super()can only be used to call parent constructor inside child constructor, not as a method call elsewhere.Final Answer:
super() cannot be called like a method in display() -> Option BQuick Check:
super() only in constructor = D [OK]
Hint: super() only calls parent constructor inside child constructor [OK]
Common Mistakes:
- Using super() outside constructor
- Thinking super.show() is invalid
- Believing overriding is not allowed
5. Given these classes:
What is the output when running
class Animal {
String name;
Animal(String name) {
this.name = name;
}
void sound() {
System.out.println("Animal sound");
}
}
class Dog extends Animal {
Dog() {
super("Dog");
}
void sound() {
super.sound();
System.out.println("Bark");
}
}
public class Test {
public static void main(String[] args) {
Dog d = new Dog();
d.sound();
System.out.println(d.name);
}
}What is the output when running
Test.main()?hard
Solution
Step 1: Understand constructor chaining
TheDogconstructor callssuper("Dog"), settingnameto "Dog" inAnimal.Step 2: Trace the
sound()method callDog.sound()callssuper.sound()which prints "Animal sound", then prints "Bark".Step 3: Print the
Printingnamefieldd.nameoutputs "Dog".Final Answer:
Animal sound Bark Dog -> Option AQuick Check:
super() sets name, super.sound() prints parent sound = A [OK]
Hint: super() sets parent state; super.method() calls parent method [OK]
Common Mistakes:
- Expecting Dog before Animal sound
- Confusing order of prints
- Thinking super() causes error
