Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is the purpose of the super keyword in Java?
The <code>super</code> keyword is used to refer to the parent class of the current object. It helps access parent class methods, constructors, and variables.
Click to reveal answer
beginner
How do you call a parent class constructor using <code>super</code>?
You call a parent class constructor by writing <code>super(arguments);</code> as the first line inside the child class constructor.
Click to reveal answer
intermediate
Can <code>super</code> be used to access parent class variables if they are hidden by child class variables?
Yes, <code>super.variableName</code> accesses the parent class variable when the child class has a variable with the same name.
Click to reveal answer
intermediate
What happens if you don't explicitly call <code>super()</code> in a child class constructor?
Java automatically inserts a call to the no-argument parent constructor super() if you don't call it explicitly. If the parent has no no-arg constructor, it causes a compile-time error.
Click to reveal answer
beginner
Can super be used to call overridden methods in the parent class?
Yes, <code>super.methodName()</code> calls the parent class version of a method that is overridden in the child class.
Click to reveal answer
What does super() do inside a child class constructor?
ACalls the child class constructor
BCreates a new object
CCalls a method named super
DCalls the parent class constructor
✗ Incorrect
super() calls the parent class constructor to initialize the parent part of the object.
How do you access a parent class variable hidden by a child class variable?
AUse <code>super.variableName</code>
BUse <code>this.variableName</code>
CUse <code>parent.variableName</code>
DUse <code>variableName()</code>
✗ Incorrect
super.variableName accesses the parent class variable when hidden by the child class.
If a parent class has no no-argument constructor, what happens if the child constructor does not call super() explicitly?
ACode compiles fine
BCode throws a runtime exception
CCode fails to compile
DParent constructor is called automatically with arguments
✗ Incorrect
Java inserts super() automatically if not called explicitly, so if no no-arg constructor exists, compilation fails.
Which of these can super NOT be used for?
ACreating new objects
BAccessing parent class variables
CCalling parent class methods
DCalling parent class constructors
✗ Incorrect
super is for accessing parent class members, not for creating new objects.
How do you call a parent class method that is overridden in the child class?
A<code>this.methodName()</code>
B<code>super.methodName()</code>
C<code>parent.methodName()</code>
D<code>methodName()</code>
✗ Incorrect
super.methodName() calls the parent class version of an overridden method.
Explain how the super keyword helps in constructor chaining in Java inheritance.
Think about how child and parent constructors work together.
You got /4 concepts.
Describe how super can be used to access hidden variables and overridden methods in a child class.
Consider when child class has same names as parent.
You got /4 concepts.
Practice
(1/5)
1. What does the super keyword do in Java?
easy
A. It defines a static method in the class.
B. It creates a new object of the child class.
C. It accesses methods and variables from the parent class.
D. It terminates the program execution.
Solution
Step 1: Understand the role of super
The super keyword 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 C
Quick 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
A. super();
B. this();
C. parent();
D. base();
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 keyword super(). Others are invalid or refer to different concepts.
Final Answer:
super(); -> Option A
Quick 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
A. 20
B. Compilation error
C. 0
D. 10
Solution
Step 1: Understand variable hiding and super usage
The child class has its own x = 20, but super.x accesses the parent's x which is 10.
Step 2: Trace the print statement
The method printX() prints super.x, so it prints 10.
Final Answer:
10 -> Option D
Quick Check:
super.x accesses parent variable = 10 [OK]
Hint: super.variable accesses parent class variable [OK]