Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to override the method display in the subclass.
Java
class Parent { void display() { System.out.println("Parent display"); } } class Child extends Parent { @Override void [1]() { System.out.println("Child display"); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using a different method name in the subclass.
Forgetting the @Override annotation (though optional, it helps).
β Incorrect
The method name in the subclass must exactly match the method name in the parent class to override it.
2fill in blank
mediumComplete the code to call the overridden method from the subclass object.
Java
class Parent { void greet() { System.out.println("Hello from Parent"); } } class Child extends Parent { @Override void greet() { System.out.println("Hello from Child"); } } public class Test { public static void main(String[] args) { Child obj = new Child(); obj.[1](); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Calling a method name that does not exist in the class.
Using the parent class method name incorrectly.
β Incorrect
To call the overridden method, use the exact method name defined in the class, which is
greet.3fill in blank
hardFix the error in the method overriding by completing the method signature correctly.
Java
class Animal { void sound() { System.out.println("Animal sound"); } } class Dog extends Animal { @Override public [1] sound() { System.out.println("Bark"); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Changing the return type in the overriding method.
Adding a return statement when the parent method returns void.
β Incorrect
The return type of the overriding method must match the parent method's return type, which is void here.
4fill in blank
hardFill both blanks to override the method and call the parent class method inside the overriding method.
Java
class Vehicle { void start() { System.out.println("Vehicle started"); } } class Car extends Vehicle { @Override void [1]() { super.[2](); System.out.println("Car started"); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using different method names in override and super call.
Forgetting to call the parent method if needed.
β Incorrect
The method name must be
start to override, and super.start() calls the parent method.5fill in blank
hardFill all three blanks to override the method with a parameter and call the parent method with the same parameter.
Java
class Printer { void print(String message) { System.out.println("Printer: " + message); } } class ColorPrinter extends Printer { @Override void [1](String [2]) { super.[3]([2]); System.out.println("ColorPrinter: " + [2]); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using different method or parameter names.
Not passing the parameter to the parent method.
β Incorrect
The method name is
print, the parameter name is message, and the parent method is called with super.print(message).