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 public 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 (not mandatory but recommended).
β Incorrect
The method name in the subclass must exactly match the method name in the parent class to override it. Here,
display is the correct method name.2fill in blank
mediumComplete the code to override the method with the correct return type.
Java
class Parent { int getNumber() { return 10; } } class Child extends Parent { @Override public [1] getNumber() { return 20; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Changing the return type to a different incompatible type.
Using
void when the parent method returns a value.β Incorrect
The return type in the overriding method must be the same or a subtype (covariant) of the parent method's return type. Here,
int matches exactly.3fill in blank
hardFix the error in the method overriding by choosing the correct access modifier.
Java
class Parent { protected void show() { System.out.println("Parent show"); } } class Child extends Parent { @Override [1] void show() { System.out.println("Child show"); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using
private in the child method which is more restrictive.Using
final which prevents overriding.β Incorrect
The overriding method cannot have a more restrictive access modifier. Since the parent method is
protected, the child method must be protected or more accessible.4fill in blank
hardFill both blanks to correctly override the method and handle exceptions.
Java
class Parent { void process() throws [1] { System.out.println("Parent process"); } } class Child extends Parent { @Override void process() throws [2] { System.out.println("Child process"); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Throwing a broader exception in the child method.
Throwing an unrelated exception.
β Incorrect
The child method can only throw the same or narrower exceptions.
FileNotFoundException is a subclass of IOException, so this is allowed.5fill in blank
hardFill all three blanks to correctly override the method with covariant return type and access modifier.
Java
class Parent { Number getValue() { return 10; } } class Child extends Parent { @Override public [1] getValue() { return new [2](); } } class [3] extends Number { public int intValue() { return 0; } public long longValue() { return 0L; } public float floatValue() { return 0.0f; } public double doubleValue() { return 0.0; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using a return type not related to
Number.Using a more restrictive access modifier than the parent method.
β Incorrect
The overriding method must have the same or more accessible modifier (here
public is already set), the return type can be a subclass of the parent's return type (covariant). MyNumber extends Number and is returned.