0
0
Javaprogramming~20 mins

Method overriding rules in Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Method Overriding Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of overridden method with super call
What is the output of this Java program?
Java
class Parent {
    void show() {
        System.out.println("Parent show");
    }
}

class Child extends Parent {
    @Override
    void show() {
        System.out.println("Child show");
        super.show();
    }
}

public class Test {
    public static void main(String[] args) {
        Parent obj = new Child();
        obj.show();
    }
}
AParent show\nChild show
BChild show\nParent show
CChild show
DParent show
Attempts:
2 left
πŸ’‘ Hint
Remember that the overridden method in the child class calls super.show() after printing.
❓ Predict Output
intermediate
2:00remaining
Return type covariance in method overriding
What will be the output when this Java code runs?
Java
class Animal {}
class Dog extends Animal {}

class Parent {
    Animal getAnimal() {
        System.out.println("Parent getAnimal");
        return new Animal();
    }
}

class Child extends Parent {
    @Override
    Dog getAnimal() {
        System.out.println("Child getAnimal");
        return new Dog();
    }
}

public class Test {
    public static void main(String[] args) {
        Parent p = new Child();
        Animal a = p.getAnimal();
    }
}
AChild getAnimal\n
BParent getAnimal\n
CCompilation error due to return type mismatch
DRuntime error
Attempts:
2 left
πŸ’‘ Hint
Check if the return type in Child is allowed to be a subclass of Parent's return type.
❓ Predict Output
advanced
2:00remaining
Access modifier rules in method overriding
What error or output occurs when compiling and running this code?
Java
class Parent {
    protected void display() {
        System.out.println("Parent display");
    }
}

class Child extends Parent {
    @Override
    void display() {
        System.out.println("Child display");
    }
}

public class Test {
    public static void main(String[] args) {
        Parent p = new Child();
        p.display();
    }
}
ARuntime error
BChild display
CParent display
DCompilation error: attempting to assign weaker access privileges
Attempts:
2 left
πŸ’‘ Hint
Check if the access modifier in Child's display is compatible with Parent's.
❓ Predict Output
advanced
2:00remaining
Exception rules in overridden methods
What happens when compiling this code?
Java
class Parent {
    void process() throws Exception {
        System.out.println("Parent process");
    }
}

class Child extends Parent {
    @Override
    void process() throws RuntimeException {
        System.out.println("Child process");
    }
}

public class Test {
    public static void main(String[] args) {
        Parent p = new Child();
        try {
            p.process();
        } catch (Exception e) {
            System.out.println("Caught Exception");
        }
    }
}
ARuntimeException thrown at runtime
BCompilation error: overridden method throws broader exception
CChild process
DCaught Exception
Attempts:
2 left
πŸ’‘ Hint
Check if RuntimeException is allowed as an overridden method exception.
🧠 Conceptual
expert
2:00remaining
Which statement about method overriding is true?
Select the only true statement about method overriding in Java.
AAn overriding method must have the same return type or a subtype of the overridden method's return type.
BAn overriding method can have a more restrictive access modifier than the overridden method.
CStatic methods can be overridden like instance methods.
DAn overriding method can throw any checked exception regardless of the overridden method's exceptions.
Attempts:
2 left
πŸ’‘ Hint
Think about access modifiers, exceptions, return types, and static methods rules.