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
Recall & Review
beginner
What is method overriding in Java?
Method overriding occurs when a subclass provides its own version of a method that is already defined in its superclass. It allows the subclass to change or extend the behavior of that method.
Click to reveal answer
intermediate
What are the rules for method overriding in Java?
The method in the subclass must have the same name, return type, and parameters as the method in the superclass. The overriding method cannot have more restrictive access than the overridden method.
Click to reveal answer
beginner
Can a private method be overridden in Java?
No, private methods cannot be overridden because they are not visible to subclasses. They are only accessible within the class they are declared in.
Click to reveal answer
beginner
What is the use of the @Override annotation?
The @Override annotation tells the compiler that the method is intended to override a method in the superclass. It helps catch errors if the method does not correctly override any method.
Click to reveal answer
intermediate
What happens if you call an overridden method on a superclass reference pointing to a subclass object?
The subclass's version of the method is called. This is called dynamic method dispatch and supports runtime polymorphism.
Click to reveal answer
Which of the following is true about method overriding?
AThe subclass method must have the same signature as the superclass method.
BThe subclass method can have a different return type than the superclass method.
CPrivate methods can be overridden.
DThe subclass method must have more restrictive access than the superclass method.
✗ Incorrect
Method overriding requires the subclass method to have the same name, parameters, and return type as the superclass method.
What does the @Override annotation do?
AIt forces the method to be private.
BIt changes the method's return type.
CIt prevents the method from being overridden.
DIt tells the compiler the method is intended to override a superclass method.
✗ Incorrect
The @Override annotation helps the compiler check that the method correctly overrides a method from the superclass.
If a superclass reference points to a subclass object, which method is called when invoking an overridden method?
AThe subclass method.
BBoth methods are called.
CThe superclass method.
DAn error occurs.
✗ Incorrect
Java uses dynamic method dispatch to call the subclass's overridden method at runtime.
Can a subclass override a static method from its superclass?
AYes, static methods can be overridden.
BOnly if the superclass method is public.
CNo, static methods cannot be overridden but can be hidden.
DOnly if the subclass method is also static.
✗ Incorrect
Static methods belong to the class, not instances, so they cannot be overridden but can be hidden by a subclass static method.
Which access modifier is NOT allowed for an overriding method compared to the overridden method?
ASame restrictive
BMore restrictive
CLess restrictive
DPublic
✗ Incorrect
The overriding method cannot have more restrictive access than the overridden method to maintain accessibility.
Explain method overriding and why it is useful in Java.
Think about how subclasses can change what a method does.
You got /4 concepts.
Describe the rules and restrictions when overriding a method in Java.
Focus on what must stay the same and what cannot be changed.
You got /4 concepts.
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
Step 1: Understand method overriding concept
Method overriding means a child class changes the behavior of a method inherited from its parent class.
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.
Final Answer:
When a child class provides a new version of a method from its parent class -> Option A
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
Step 1: Check method signature and annotation
To override, method name and parameters must match exactly. Using @Override annotation helps catch mistakes.
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.
Final Answer:
@Override void show() { System.out.println("Child"); } -> Option B
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
Step 1: Understand dynamic method dispatch
When a parent reference points to a child object, overridden methods in child are called at runtime.
Step 2: Trace the method call
obj is Parent type but refers to Child instance. Calling greet() runs Child's version.
Final Answer:
Hello from Child -> Option D
Quick Check:
Parent ref + Child object calls Child method [OK]
Hint: Parent reference calls child's overridden method at runtime [OK]