Challenge - 5 Problems
Super Keyword Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
β Predict Output
intermediateOutput of super keyword in constructor call
What is the output of this Java program that uses
super to call the parent class constructor?Java
class Parent { Parent() { System.out.println("Parent constructor"); } } class Child extends Parent { Child() { super(); System.out.println("Child constructor"); } } public class Test { public static void main(String[] args) { new Child(); } }
Attempts:
2 left
π‘ Hint
Remember that
super() calls the parent constructor before the child constructor body runs.β Incorrect
The
super() call in the Child constructor invokes the Parent constructor first, printing "Parent constructor". Then the Child constructor prints "Child constructor".β Predict Output
intermediateUsing super to access parent method
What is the output of this Java program that uses
super to call a parent class method?Java
class Animal { void sound() { System.out.println("Animal sound"); } } class Dog extends Animal { void sound() { System.out.println("Dog barks"); } void printSounds() { sound(); super.sound(); } } public class Test { public static void main(String[] args) { Dog d = new Dog(); d.printSounds(); } }
Attempts:
2 left
π‘ Hint
The
super keyword calls the parent class method, while calling the method normally calls the child class version.β Incorrect
Calling
sound() inside printSounds() calls the overridden method in Dog, printing "Dog barks". Calling super.sound() calls the Animal class method, printing "Animal sound".β Predict Output
advancedOutput when super keyword used with variables
What is the output of this Java program that uses
super to access a parent class variable?Java
class Base { int x = 10; } class Derived extends Base { int x = 20; void printX() { System.out.println(x); System.out.println(super.x); } } public class Test { public static void main(String[] args) { Derived d = new Derived(); d.printX(); } }
Attempts:
2 left
π‘ Hint
The variable
x in Derived hides the one in Base. Use super.x to access the parent's variable.β Incorrect
The
x in Derived is 20, so System.out.println(x) prints 20. The super.x accesses the Base class variable, which is 10.β Predict Output
advancedOutput of super in multi-level inheritance
What is the output of this Java program using
super in a multi-level inheritance chain?Java
class A { void show() { System.out.println("A show"); } } class B extends A { void show() { System.out.println("B show"); } } class C extends B { void show() { super.show(); System.out.println("C show"); } } public class Test { public static void main(String[] args) { C c = new C(); c.show(); } }
Attempts:
2 left
π‘ Hint
In class C,
super.show() calls the immediate parent class B's show() method.β Incorrect
The
super.show() in C calls B's show(), which prints "B show". Then C prints "C show".π§ Conceptual
expertError caused by incorrect use of super
Which option will cause a compile-time error due to incorrect use of
super in Java?Attempts:
2 left
π‘ Hint
Remember that
super relates to instance context, not static context.β Incorrect
Using
super() inside a static method causes a compile-time error because static methods do not belong to an instance, so super cannot be used there.