0
0
Javaprogramming~20 mins

Method overriding 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 call
What is the output of the following Java code?
Java
class Animal {
    void sound() {
        System.out.println("Animal sound");
    }
}

class Dog extends Animal {
    @Override
    void sound() {
        System.out.println("Bark");
    }
}

public class Main {
    public static void main(String[] args) {
        Animal a = new Dog();
        a.sound();
    }
}
ABark
BRuntime error
CCompilation error
DAnimal sound
Attempts:
2 left
πŸ’‘ Hint
Remember that the method called depends on the actual object type, not the reference type.
❓ Predict Output
intermediate
2:00remaining
Output with super keyword in overridden method
What will be printed when this Java program runs?
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 Main {
    public static void main(String[] args) {
        Child c = new Child();
        c.show();
    }
}
AChild show
B
Child show
Parent show
C
Parent show
Child show
DParent show
Attempts:
2 left
πŸ’‘ Hint
The super keyword calls the parent class method inside the overridden method.
πŸ”§ Debug
advanced
2:00remaining
Identify the error in method overriding
Which option correctly identifies the error in this code snippet?
Java
class Base {
    void display() {}
}

class Derived extends Base {
    int display() { return 1; }
}
ARuntime error due to return type mismatch
BCompilation error because method name is different
CNo error, code runs fine
DCompilation error because return type int does not match void
Attempts:
2 left
πŸ’‘ Hint
Check if the return type matches when overriding a method.
🧠 Conceptual
advanced
2:00remaining
Effect of private method on overriding
What happens if a method in the parent class is private and a method with the same signature is declared in the child class?
AThe child method is a new method, no overriding occurs
BThe child method overrides the parent private method
CCompilation error due to duplicate method
DRuntime error due to access violation
Attempts:
2 left
πŸ’‘ Hint
Private methods are not visible to child classes.
❓ Predict Output
expert
2:00remaining
Output with method overriding and casting
What is the output of this Java program?
Java
class A {
    void print() {
        System.out.println("A");
    }
}

class B extends A {
    @Override
    void print() {
        System.out.println("B");
    }
}

class C extends B {
    @Override
    void print() {
        System.out.println("C");
    }
}

public class Main {
    public static void main(String[] args) {
        A obj = new C();
        ((B) obj).print();
    }
}
AClassCastException at runtime
BB
CC
DA
Attempts:
2 left
πŸ’‘ Hint
Casting does not change the actual object type; overridden method of actual object is called.