Bird
Raised Fist0
Javaprogramming~10 mins

Super keyword in Java - Interactive Code Practice

Choose your learning style10 modes available

Start learning this pattern below

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
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to call the parent class constructor.

Java
class Parent {
    Parent() {
        System.out.println("Parent constructor called");
    }
}

class Child extends Parent {
    Child() {
        [1]();
        System.out.println("Child constructor called");
    }
}

public class Main {
    public static void main(String[] args) {
        new Child();
    }
}
Drag options to blanks, or click blank then click option'
Asuper
Bthis
Cparent
Dbase
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using 'this()' instead of 'super()' to call the parent constructor.
Trying to call 'parent()' which is not a valid keyword.
2fill in blank
medium

Complete the code to access the parent class method from the child class.

Java
class Parent {
    void show() {
        System.out.println("Parent show method");
    }
}

class Child extends Parent {
    void show() {
        [1].show();
        System.out.println("Child show method");
    }
}

public class Main {
    public static void main(String[] args) {
        Child c = new Child();
        c.show();
    }
}
Drag options to blanks, or click blank then click option'
Athis
Bparent
Csuper
Dbase
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using 'this.show()' which calls the child method recursively.
Using 'parent.show()' which is not valid in Java.
3fill in blank
hard

Fix the error in the constructor call to the parent class.

Java
class Parent {
    Parent(int x) {
        System.out.println("Parent constructor with value: " + x);
    }
}

class Child extends Parent {
    Child() {
        [1](10);
        System.out.println("Child constructor");
    }
}

public class Main {
    public static void main(String[] args) {
        new Child();
    }
}
Drag options to blanks, or click blank then click option'
Abase
Bthis
Cparent
Dsuper
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using 'this(10);' which calls the same class constructor and causes recursion.
Using 'parent(10);' which is not a valid keyword.
4fill in blank
hard

Fill the blank to override a method and call the parent method inside it.

Java
class Parent {
    void display() {
        System.out.println("Parent display");
    }
}

class Child extends Parent {
    void display() {
        [1].display();
        System.out.println("Child display");
    }
}

public class Main {
    public static void main(String[] args) {
        Child c = new Child();
        c.display();
    }
}
Drag options to blanks, or click blank then click option'
Asuper
Bthis
Cparent
Dbase
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using 'this.display();' which causes recursive call.
Using 'parent.display();' which is invalid.
5fill in blank
hard

Fill both blanks to use super keyword for constructor and method calls.

Java
class Parent {
    Parent() {
        System.out.println("Parent constructor");
    }
    void greet() {
        System.out.println("Hello from Parent");
    }
}

class Child extends Parent {
    Child() {
        [1]();
        System.out.println("Child constructor");
    }
    void greet() {
        [2].greet();
        System.out.println("Hello from Child");
    }
}

public class Main {
    public static void main(String[] args) {
        Child c = new Child();
        c.greet();
    }
}
Drag options to blanks, or click blank then click option'
Athis
Bsuper
Cparent
Dbase
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using 'this()' in constructor to call itself causing recursion.
Using 'parent' or 'base' which are invalid keywords.

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

  1. 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.
  2. 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.
  3. Final Answer:

    It accesses methods and variables from the parent class. -> Option C
  4. 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

  1. Step 1: Recall syntax for parent constructor call

    In Java, super() is used inside a child constructor to call the parent class constructor.
  2. Step 2: Evaluate options

    Only super(); uses the correct keyword super(). Others are invalid or refer to different concepts.
  3. Final Answer:

    super(); -> Option A
  4. 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

  1. 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.
  2. Step 2: Trace the print statement

    The method printX() prints super.x, so it prints 10.
  3. Final Answer:

    10 -> Option D
  4. Quick 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
A. super.show() is invalid inside child class
B. super() cannot be called like a method in display()
C. Child class cannot override show() method
D. No error, code is correct

Solution

  1. Step 1: Check usage of super in methods

    Calling super.show() inside overridden method is valid to call parent method.
  2. 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.
  3. Final Answer:

    super() cannot be called like a method in display() -> Option B
  4. Quick 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:
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
A. Animal sound Bark Dog
B. Bark Animal sound Dog
C. Animal sound Dog Bark
D. Compilation error due to super()

Solution

  1. Step 1: Understand constructor chaining

    The Dog constructor calls super("Dog"), setting name to "Dog" in Animal.
  2. Step 2: Trace the sound() method call

    Dog.sound() calls super.sound() which prints "Animal sound", then prints "Bark".
  3. Step 3: Print the name field

    Printing d.name outputs "Dog".
  4. Final Answer:

    Animal sound Bark Dog -> Option A
  5. Quick 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