0
0
Javaprogramming~10 mins

Why inheritance is used in Java - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a class that inherits from another class.

Java
class Dog extends [1] {}
Drag options to blanks, or click blank then click option'
ACar
BHouse
CTree
DAnimal
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using a class unrelated to the child class as the parent.
Forgetting to use the 'extends' keyword.
2fill in blank
medium

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

Java
class Dog extends Animal {
    Dog() {
        super[1];
    }
}
Drag options to blanks, or click blank then click option'
A()
B{}
C[]
D;
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using curly braces instead of parentheses.
Omitting parentheses after super.
3fill in blank
hard

Fix the error in the code to override a method from the parent class.

Java
class Dog extends Animal {
    @Override
    public void [1]() {
        System.out.println("Dog barks");
    }
}
Drag options to blanks, or click blank then click option'
Aspeak
Bbark
Crun
Djump
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Changing the method name when overriding.
Forgetting the @Override annotation.
4fill in blank
hard

Fill both blanks to create a subclass that inherits and adds a new method.

Java
class [1] extends Animal {
    public void [2]() {
        System.out.println("Runs fast");
    }
}
Drag options to blanks, or click blank then click option'
ACheetah
Bfly
CrunFast
DBird
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using unrelated class names.
Using method names that don't describe the action.
5fill in blank
hard

Fill all three blanks to complete the code that demonstrates inheritance and method overriding.

Java
class [1] {
    public void sound() {
        System.out.println("Animal sound");
    }
}

class [2] extends [3] {
    @Override
    public void sound() {
        System.out.println("Meow");
    }
}
Drag options to blanks, or click blank then click option'
AAnimal
BCat
DDog
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using different class names that don't match inheritance.
Not overriding the method correctly.