0
0
Javaprogramming~10 mins

Inheritance limitations 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 class that cannot be extended.

Java
public [1] class Vehicle {}
Drag options to blanks, or click blank then click option'
Astatic
Bfinal
Cabstract
Dprivate
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using 'abstract' instead of 'final' to prevent inheritance.
2fill in blank
medium

Complete the code to prevent a method from being overridden in subclasses.

Java
public class Animal {
    public [1] void sound() {
        System.out.println("Animal sound");
    }
}
Drag options to blanks, or click blank then click option'
Aabstract
Bstatic
Cprivate
Dfinal
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using 'static' which changes method behavior but does not prevent overriding.
3fill in blank
hard

Fix the error in the code by choosing the correct keyword to allow inheritance.

Java
[1] class Shape {}

public class Circle extends Shape {}
Drag options to blanks, or click blank then click option'
Apublic
Babstract
Cfinal
Dprivate
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using 'final' which prevents inheritance.
4fill in blank
hard

Fill both blanks to declare an abstract class with an abstract method.

Java
public [1] class Device {
    public [2] void start();
}
Drag options to blanks, or click blank then click option'
Aabstract
Bfinal
Dstatic
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using 'final' for abstract methods or classes.
5fill in blank
hard

Fill all three blanks to override a method and prevent further overriding.

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

public class Child extends Parent {
    @Override
    public [1] void greet() {
        System.out.println("Hello from Child");
    }
}

public class GrandChild extends Child {
    @Override
    public void greet() {
        [2].greet();
        System.out.println("Hello from GrandChild");
    }
}

// To prevent GrandChild from overriding greet(), add [3] keyword in Child class method.
Drag options to blanks, or click blank then click option'
Afinal
Bsuper
Dstatic
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Trying to override a final method causes a compile error.