Bird
Raised Fist0
Javaprogramming~20 mins

Method overriding rules 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 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.

Practice

(1/5)
1. Which of the following is true about method overriding in Java?
easy
A. The method in the child class can have fewer parameters than the parent method.
B. The method in the child class must have the same name and parameters as in the parent class.
C. The method in the child class must be static to override the parent method.
D. The method in the child class must have a different return type than the parent method.

Solution

  1. Step 1: Understand method overriding signature rules

    Method overriding requires the child method to have the exact same name and parameter list as the parent method.
  2. Step 2: Check return type and modifiers

    The return type must be the same or a subtype, and the method cannot be static to override.
  3. Final Answer:

    The method in the child class must have the same name and parameters as in the parent class. -> Option B
  4. Quick Check:

    Method signature match = D [OK]
Hint: Method name and parameters must match exactly to override [OK]
Common Mistakes:
  • Thinking return type can be different
  • Assuming static methods can be overridden
  • Changing parameter count in child method
2. Which of the following method declarations correctly overrides a parent method public int calculate(int x)?
easy
A. public int calculate(int x) { return x * 2; }
B. public void calculate(int x) { System.out.println(x); }
C. public int calculate(double x) { return (int)x; }
D. static public int calculate(int x) { return x + 1; }

Solution

  1. Step 1: Match method signature exactly

    The overriding method must have the same name and parameter types: calculate(int x).
  2. Step 2: Check return type and modifiers

    Return type must be int and method must not be static.
  3. Final Answer:

    public int calculate(int x) { return x * 2; } -> Option A
  4. Quick Check:

    Exact signature and return type = A [OK]
Hint: Match method name, parameters, and return type exactly [OK]
Common Mistakes:
  • Changing return type to void
  • Changing parameter type
  • Making method static
3. What is the output of the following code?
class Parent {
    void show() { System.out.println("Parent"); }
}
class Child extends Parent {
    @Override
    void show() { System.out.println("Child"); }
}
public class Test {
    public static void main(String[] args) {
        Parent obj = new Child();
        obj.show();
    }
}
medium
A. Parent
B. Runtime error
C. Compilation error
D. Child

Solution

  1. Step 1: Understand dynamic method dispatch

    When a parent reference points to a child object, the overridden child method is called at runtime.
  2. Step 2: Check method overriding and call

    The show() method is overridden in Child, so obj.show() calls Child's version.
  3. Final Answer:

    Child -> Option D
  4. Quick Check:

    Overridden method called at runtime = B [OK]
Hint: Overridden method runs, not parent, when using child object [OK]
Common Mistakes:
  • Expecting parent method output
  • Confusing compile-time and runtime method calls
  • Ignoring @Override annotation effect
4. Identify the error in the following code snippet:
class Parent {
    void display() {}
}
class Child extends Parent {
    @Override
    void display(int x) {}
}
medium
A. Cannot use @Override annotation on any method.
B. Missing return type in Child's display method.
C. Method display(int x) does not override display() due to different parameters.
D. Child class cannot have methods with parameters.

Solution

  1. Step 1: Compare method signatures in Parent and Child

    Parent has display() with no parameters; Child has display(int x) with one parameter.
  2. Step 2: Understand @Override annotation rules

    @Override requires exact signature match; here, parameters differ, so it's not overriding.
  3. Final Answer:

    Method display(int x) does not override display() due to different parameters. -> Option C
  4. Quick Check:

    @Override requires exact signature match = C [OK]
Hint: @Override needs exact method signature match to avoid errors [OK]
Common Mistakes:
  • Ignoring parameter difference
  • Thinking @Override can be used on any method
  • Assuming method overloading is overriding
5. Consider this code:
class Animal {
    Number getValue() { return 10; }
}
class Dog extends Animal {
    @Override
    Integer getValue() { return 20; }
}

Which statement about this overriding is correct?
hard
A. This is valid because Integer is a subclass of Number (covariant return type).
B. This causes a compile-time error due to different return types.
C. This is invalid because return types must be exactly the same.
D. This is invalid because @Override cannot be used with different return types.

Solution

  1. Step 1: Check return types in parent and child methods

    Parent returns Number, child returns Integer, which is a subclass of Number.
  2. Step 2: Understand covariant return types in Java overriding

    Java allows child methods to return a subtype of the parent's return type when overriding.
  3. Final Answer:

    This is valid because Integer is a subclass of Number (covariant return type). -> Option A
  4. Quick Check:

    Covariant return types allowed = A [OK]
Hint: Child can return subtype of parent's return type when overriding [OK]
Common Mistakes:
  • Thinking return types must be exactly the same
  • Assuming @Override forbids different return types
  • Confusing overloading with overriding