Bird
Raised Fist0
Javaprogramming~20 mins

Method overriding in Java - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1.

What is method overriding in Java?

easy
A. When a child class provides a new version of a method from its parent class
B. When a method calls itself repeatedly
C. When two methods have the same name but different parameters
D. When a method is hidden from other classes

Solution

  1. Step 1: Understand method overriding concept

    Method overriding means a child class changes the behavior of a method inherited from its parent class.
  2. Step 2: Compare options with definition

    When a child class provides a new version of a method from its parent class correctly describes this. When a method calls itself repeatedly is recursion, C is method overloading, D is incorrect.
  3. Final Answer:

    When a child class provides a new version of a method from its parent class -> Option A
  4. Quick Check:

    Method overriding = child changes parent method [OK]
Hint: Overriding means child changes parent's method behavior [OK]
Common Mistakes:
  • Confusing overriding with overloading
  • Thinking overriding changes method signature
  • Mixing overriding with recursion
2.

Which of the following is the correct syntax to override a method in Java?

class Parent {
    void show() { System.out.println("Parent"); }
}
class Child extends Parent {
    ?
}
easy
A. private void show() { System.out.println("Child"); }
B. @Override void show() { System.out.println("Child"); }
C. void Show() { System.out.println("Child"); }
D. void show(int x) { System.out.println("Child"); }

Solution

  1. Step 1: Check method signature and annotation

    To override, method name and parameters must match exactly. Using @Override annotation helps catch mistakes.
  2. Step 2: Analyze options

    private void show() { System.out.println("Child"); } uses private access modifier, narrowing from package-private (not allowed for overriding; causes hiding). void show(int x) { System.out.println("Child"); } changes parameters (overloading). void Show() { System.out.println("Child"); } has method name mismatch ('Show' vs 'show' due to case sensitivity). @Override void show() { System.out.println("Child"); } matches method signature exactly and uses @Override correctly.
  3. Final Answer:

    @Override void show() { System.out.println("Child"); } -> Option B
  4. Quick Check:

    @Override + same signature = correct override [OK]
Hint: Use @Override and same method signature to override [OK]
Common Mistakes:
  • Narrowing access modifier (e.g., to private)
  • Changing method parameters (overloading instead)
  • Method name case mismatch
3.

What will be the output of the following code?

class Parent {
    void greet() { System.out.println("Hello from Parent"); }
}
class Child extends Parent {
    @Override
    void greet() { System.out.println("Hello from Child"); }
}
public class Test {
    public static void main(String[] args) {
        Parent obj = new Child();
        obj.greet();
    }
}
medium
A. Hello from Parent
B. Compilation error
C. Runtime error
D. Hello from Child

Solution

  1. Step 1: Understand dynamic method dispatch

    When a parent reference points to a child object, overridden methods in child are called at runtime.
  2. Step 2: Trace the method call

    obj is Parent type but refers to Child instance. Calling greet() runs Child's version.
  3. Final Answer:

    Hello from Child -> Option D
  4. Quick Check:

    Parent ref + Child object calls Child method [OK]
Hint: Parent reference calls child's overridden method at runtime [OK]
Common Mistakes:
  • Expecting parent method output
  • Confusing compile-time and runtime method binding
  • Thinking method overloading applies here
4.

Identify the error in the following code snippet:

class Parent {
    void display() { System.out.println("Parent"); }
}
class Child extends Parent {
    @Override
    void Display() { System.out.println("Child"); }
}
medium
A. Method name case mismatch causes no overriding
B. Missing return type in Child class method
C. Cannot override a method without parameters
D. No error, code is correct

Solution

  1. Step 1: Check method names carefully

    Parent method is 'display' (lowercase d), Child method is 'Display' (uppercase D). Java is case-sensitive, so these are different methods.
  2. Step 2: Understand @Override annotation effect

    @Override expects exact match. Here, it causes a compile-time error because method names differ in case.
  3. Final Answer:

    Method name case mismatch causes no overriding -> Option A
  4. Quick Check:

    Method names must match exactly, including case [OK]
Hint: Method names must match case exactly to override [OK]
Common Mistakes:
  • Ignoring case sensitivity in method names
  • Assuming @Override is optional and ignoring errors
  • Confusing overriding with overloading
5.

Consider the following classes:

class Animal {
    String sound() { return "Some sound"; }
}
class Dog extends Animal {
    @Override
    String sound() { return "Bark"; }
}
class Cat extends Animal {
    @Override
    String sound() { return "Meow"; }
}
public class Test {
    public static void main(String[] args) {
        Animal[] animals = {new Dog(), new Cat(), new Animal()};
        for (Animal a : animals) {
            System.out.println(a.sound());
        }
    }
}

What is the output of this program?

hard
A. Bark Some sound Meow
B. Some sound Some sound Some sound
C. Bark Meow Some sound
D. Compilation error due to array of different types

Solution

  1. Step 1: Understand polymorphism with overridden methods

    Each object calls its own overridden sound() method at runtime, even if referenced as Animal.
  2. Step 2: Trace the loop output

    Dog's sound() returns "Bark", Cat's returns "Meow", Animal's returns "Some sound".
  3. Final Answer:

    Bark Meow Some sound -> Option C
  4. Quick Check:

    Array of Animals calls each overridden sound() correctly [OK]
Hint: Array of parent type calls each child's overridden method [OK]
Common Mistakes:
  • Expecting all calls to use parent method
  • Thinking array cannot hold different subclass objects
  • Confusing overriding with overloading