When overriding a method, can a child class throw an additional exception not declared by the parent?
Yes - but only if the additional exception is unchecked (i.e., a subclass of RuntimeException).
What is an unchecked exception in Java?
An exception that extends RuntimeException or Error. It does not need to be declared with throws and is not checked by the compiler at compile time.
What is a checked exception in Java?
An exception that extends Exception (but not RuntimeException). It must be declared in the method signature with throws or handled with try-catch.
Can a child class add a new checked exception when overriding a parent method?
No. A child cannot throw a new or broader checked exception that the parent method does not already declare.
Can a child class throw a narrower checked exception than the parent?
Yes. The child can throw the same checked exception or a subclass of it.
Can a child class remove a checked exception entirely when overriding?
Yes. The child is free to not declare any checked exception, even if the parent does.
Can a child class add RuntimeException even if the parent method throws nothing?
Yes. Unchecked exceptions can always be added or removed freely, regardless of what the parent declares.
Why can a child freely add unchecked exceptions?
Because unchecked exceptions are not part of the method contract. Callers are not required to handle them, so they do not break the parent’s API.
Why can’t a child add new checked exceptions?
Because callers that reference the parent type are only prepared to handle what the parent declares. Adding a new checked exception would break polymorphism.
What principle does this rule protect?
The Liskov Substitution Principle (LSP) - a child class must be safely substitutable for its parent without breaking existing code.
What does the compiler do if you try to add a broader checked exception in an override?
It produces a compile-time error: Exception X is not compatible with throws clause in Parent.method().
VALID or INVALID? Parent: void save() throws IOException; Child: void save() throws FileNotFoundException;
VALID - FileNotFoundException is a subclass of IOException (narrower checked exception).
VALID or INVALID? Parent: void save() throws IOException; Child: void save() throws Exception;
INVALID - Exception is broader than IOException. The compiler will reject this.
VALID or INVALID? Parent: void save(); Child: void save() throws NullPointerException;
VALID - NullPointerException is unchecked. Unchecked exceptions can always be added freely.
VALID or INVALID? Parent: void save() throws IOException; Child: void save();
VALID - The child is allowed to remove the checked exception entirely.
Do these override exception rules apply to unchecked exceptions?
No. These rules only govern checked exceptions. Unchecked exceptions are unrestricted in overrides.
Do these rules apply to static methods?
No. Static methods cannot be overridden - they are hidden. The override exception rules do not apply.
Does the compiler enforce override exception rules?
Yes. Violations of checked exception rules in method overrides are caught at compile time.
What is the key rule to remember for exception handling in method overrides?
A child method can throw the same, narrower, or no checked exceptions - and any unchecked exceptions freely. It cannot throw new or broader checked exceptions.
What are the main benefits of the Java Collections Framework?
Reusability - Quality - Speed - Maintenance
What does Reusability mean in the context of the Collections Framework?
The framework provides common classes and utility methods that work across different collection types so developers reuse proven code instead of writing their own from scratch
What does Quality mean in the context of the Collections Framework?
Collections Framework code is battle-tested and used by millions of developers worldwide meaning it is reliable and far less likely to contain bugs than custom-written alternatives
How does the Collections Framework improve development Speed?
Developers can focus on core business logic instead of writing low-level data structure code which significantly increases productivity and shortens development time
How does the Collections Framework help with Maintenance?
The framework is open source with widely available API documentation making it easy for any developer to read understand and maintain code written by someone else