0
0
Javaprogramming~10 mins

Method overriding 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 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'
Aoutput
Bshow
Cprint
Ddisplay
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using a different method name in the subclass.
Forgetting the @Override annotation (though optional, it helps).
2fill in blank
medium

Complete 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'
Agreet
BsayHello
Chello
Ddisplay
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Calling a method name that does not exist in the class.
Using the parent class method name incorrectly.
3fill in blank
hard

Fix 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'
Adouble
Bvoid
CString
Dint
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Changing the return type in the overriding method.
Adding a return statement when the parent method returns void.
4fill in blank
hard

Fill 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'
Astart
Brun
Dbegin
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using different method names in override and super call.
Forgetting to call the parent method if needed.
5fill in blank
hard

Fill 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'
Aprint
Bmessage
Dtext
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using different method or parameter names.
Not passing the parameter to the parent method.