0
0
Javaprogramming~10 mins

Super keyword in Java - Interactive Code Practice

Choose your learning style9 modes available
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.