~ Mansukh
Campbell Ritchie wrote:That has nothing to do with signatures. That has to do with trying to load a non‑existent class. I think a throws clause is not actually part of the signature. You would have to check override‑equivalence in the java Language Specification.
~ Mansukh
Campbell Ritchie wrote:Do you mean what does the JLS say, or what is the right way to override methods?
The JLS will tell you that it is a compile‑time error for an overriding method to specify a checked exception unless it is a subtype of an Exception specified by the overridden method.
The correct way to override a method is not to specify or throw any Exceptions which are not thrown by the overridden method. That means the methods like equals() and toString() which do not throw any Exceptions when invoked on an instance of java.lang.Object, should never throw Exceptions of any sort in subtypes. The reason is to do with the Liskov Substitution Principle. If a method of a subtype throws a “new” Exception, then the subtype is not behaving as the supertype object would. Or, using Abrial’s characteristic predicates, ∀ m • fis(msupertype) ⇒ fis(msbrtype).
But the compiler only looks for checked Exceptions. The fact that the compiler will not notice a toString() method like this does not mean it is right:-
~ Mansukh
Mansukhdeep Thind wrote:
What are these "Liskov Substitution" principle and Abrial characteristic predicates. Could you perhaps provide some link where I can educate myself regarding these, now that we have dived into the reasons.
K. Tsang CEng MBCS PMP PMI-ACP OCMJEA OCPJP
K. Tsang wrote:
Mansukhdeep Thind wrote:
What are these "Liskov Substitution" principle and Abrial characteristic predicates. Could you perhaps provide some link where I can educate myself regarding these, now that we have dived into the reasons.
There are a few OO design principles which can be found here.
~ Mansukh
Campbell Ritchie wrote:The characteristic predicates are at a much more fundamental level than principles of OO.
~ Mansukh
Campbell Ritchie wrote:Learn about the Liskov Substitution Principle now. Leave characteristic predicates until you know some generalised substitution language. Which is an entirely theoretical language used to reasoning about programs, not for creating programs.
The little predicate I wrote means that in all cases where the superclass method will run, the subclass method will run, too.
~ Mansukh