0
0
Javaprogramming~10 mins

Parent and child classes 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 declare a child class that inherits from a parent class.

Java
class Child [1] Parent {}
Drag options to blanks, or click blank then click option'
Aimplements
Bextends
Cinherits
Duses
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using 'implements' instead of 'extends' for class inheritance.
Using 'inherits' which is not a Java keyword.
2fill in blank
medium

Complete the code to call the parent class constructor from the child class constructor.

Java
class Child extends Parent {
    Child() {
        [1]();
    }
}
Drag options to blanks, or click blank then click option'
Asuper
Bthis
Cparent
Dbase
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using 'this()' which calls the current class constructor.
Using 'parent()' or 'base()' which are not Java keywords.
3fill in blank
hard

Fix the error in the child class method overriding the parent method.

Java
class Parent {
    void greet() {
        System.out.println("Hello from Parent");
    }
}

class Child extends Parent {
    @Override
    void [1]() {
        System.out.println("Hello from Child");
    }
}
Drag options to blanks, or click blank then click option'
Ahello
Bgreeting
CsayHello
Dgreet
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Changing the method name so it does not override the parent method.
Missing the @Override annotation (not an error but good practice).
4fill in blank
hard

Fill both blanks to create a constructor in the child class that calls the parent constructor with a parameter.

Java
class Parent {
    Parent(String name) {
        System.out.println("Parent name: " + name);
    }
}

class Child extends Parent {
    Child(String name) {
        [1]([2]);
    }
}
Drag options to blanks, or click blank then click option'
Asuper
Bthis
Cname
D"John"
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using 'this' instead of 'super' to call the parent constructor.
Passing a string literal instead of the parameter.
5fill in blank
hard

Fill all three blanks to override a method in the child class and call the parent method inside it.

Java
class Parent {
    void display() {
        System.out.println("Parent display");
    }
}

class Child extends Parent {
    @Override
    void [1]() {
        [2].[3]();
        System.out.println("Child display");
    }
}
Drag options to blanks, or click blank then click option'
Adisplay
Bsuper
Dthis
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using 'this' instead of 'super' to call the parent method.
Changing the method name so it does not override.