0
0
Javaprogramming~10 mins

Method overriding rules 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
    public void [1]() {
        System.out.println("Child display");
    }
}
Drag options to blanks, or click blank then click option'
Ashow
Bprint
Cdisplay
DdisplayMessage
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using a different method name in the subclass.
Forgetting the @Override annotation (not mandatory but recommended).
2fill in blank
medium

Complete 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'
Aint
Bvoid
CString
Ddouble
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Changing the return type to a different incompatible type.
Using void when the parent method returns a value.
3fill in blank
hard

Fix 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'
Afinal
Bdefault
Cprivate
Dprotected
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using private in the child method which is more restrictive.
Using final which prevents overriding.
4fill in blank
hard

Fill 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'
AIOException
BException
CFileNotFoundException
DRuntimeException
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Throwing a broader exception in the child method.
Throwing an unrelated exception.
5fill in blank
hard

Fill 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'
ANumber
BInteger
CMyNumber
Dprotected
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using a return type not related to Number.
Using a more restrictive access modifier than the parent method.