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 a specific implementation for a method that is already defined in its superclass with the same name, return type, and parameters.
Click to reveal answer
intermediate
Can the access modifier of an overriding method be more restrictive than the overridden method?
No, the overriding method cannot have a more restrictive access modifier. It can have the same or a less restrictive modifier to maintain accessibility.
Click to reveal answer
intermediate
Is it allowed to override a static method in Java?
No, static methods cannot be overridden. They can be hidden by another static method in the subclass, but this is not true overriding.
Click to reveal answer
advanced
What happens if the overriding method throws a checked exception not declared in the overridden method?
The overriding method cannot throw new or broader checked exceptions than the overridden method. It can throw fewer or narrower exceptions or none at all.
Click to reveal answer
intermediate
Can the return type of the overriding method differ from the overridden method?
Yes, since Java 5, the overriding method can have a covariant return type, meaning it can return a subtype of the original method's return type.
Click to reveal answer
Which of the following is true about method overriding in Java?
AStatic methods can be overridden like instance methods.
BThe overriding method can have a different name but same parameters.
CThe overriding method can have a more restrictive access modifier.
DThe overriding method must have the same name, parameters, and return type (or subtype) as the overridden method.
✗ Incorrect
Method overriding requires the same method name and parameters. The return type can be the same or a subtype (covariant). Access modifier cannot be more restrictive. Static methods cannot be overridden.
If a superclass method throws IOException, what exceptions can the overriding subclass method throw?
AIOException or its subclasses only.
BNo exceptions at all.
CAny checked exception including SQLException.
DAny unchecked exception only.
✗ Incorrect
The overriding method can throw the same checked exception or its subclasses, or no exception. It cannot throw new or broader checked exceptions.
What happens if you try to override a private method in Java?
AIt overrides the private method successfully.
BIt creates a new method unrelated to the superclass method.
CIt causes a compile-time error.
DIt overrides the method only if the subclass is in the same package.
✗ Incorrect
Private methods are not visible to subclasses, so defining a method with the same signature creates a new unrelated method, not an override.
Which access modifier is allowed for an overriding method if the original method is declared as protected?
Aprivate
Bdefault (package-private)
Cprotected or public
DAny access modifier
✗ Incorrect
The overriding method can have the same or less restrictive access modifier. Since protected is less restrictive than private and default, it can be protected or public.
Can a final method be overridden in Java?
ANo, final methods cannot be overridden.
BOnly if the method is static.
COnly if the subclass is in the same package.
DYes, final methods can be overridden.
✗ Incorrect
Final methods cannot be overridden to prevent modification of their behavior.
Explain the key rules you must follow when overriding a method in Java.
Think about method signature, access, exceptions, and special method types.
You got /5 concepts.
Describe what happens if you try to override a private or static method in Java.
Consider visibility and method type differences.
You got /4 concepts.
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.