Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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.
β Incorrect
The keyword 'super' is used to call the parent class constructor in Java.
2fill in blank
mediumComplete 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'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using 'this.show()' which calls the child method recursively.
Using 'parent.show()' which is not valid in Java.
β Incorrect
The 'super' keyword is used to call the parent class method from the child class.
3fill in blank
hardFix 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'
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.
β Incorrect
Use 'super(10);' to call the parent constructor with an argument.
4fill in blank
hardFill 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'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using 'this.display();' which causes recursive call.
Using 'parent.display();' which is invalid.
β Incorrect
The 'super' keyword is used to call the parent class method from the overridden method in the child class.
5fill in blank
hardFill 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'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using 'this()' in constructor to call itself causing recursion.
Using 'parent' or 'base' which are invalid keywords.
β Incorrect
Use 'super()' to call the parent constructor and 'super.greet()' to call the parent method.