0
0
Javaprogramming~5 mins

Method overriding rules in Java - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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.
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.
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.
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
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.
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.