Given a method that doesn declare any exceptions, can I override that method in a subclass to throw an exception?
Ans : No; subclasses must honor the API contract established by their superclasses, and this includes the types of checked exceptions that a method can throw. An API (application programming interface) establishes a contract of intent, not just of form or interpretation. To borrow terminology from linguistics and philosophy, an API contract involves both extension and intension: the boundaries of the current state of the world (extension) as well as the intended boundaries for other possible states of the world (intension: possible future implementations). In object-oriented programming, a common source of “possible future implementations” is subclassing from an existing class in an API. A method defines a contract for any subclass method that would override it; it constrains possible implementations that a subclass could provide. In Java, the bare minimum contract for a method’s inputs and outputs is the following: • A method’s parameter list is fixed; an overriding method in a subcla
Related Questions
- We can declare methods that throw an exception (or error) of unknown type. Example (of method with type parameter in throws clause): interface Task< E extends Exception > { void run() throws E ; } Can such a method throw an object of the unknown exception (or error) type?
- I am not receiving data exception messages but I am receiving data exceptions. What is the best method to analyze a data exception error to determine where the error occurs?
- Given a method that doesn declare any exceptions, can I override that method in a subclass to throw an exception?