Jeff Verdegan wrote:First tell us what you think.
Vaibhav G Garg wrote:
Jeff Verdegan wrote:First tell us what you think.
i. We can not restrict the method access i.e from public to private/protected or from protected to private. Why it is so?
<Vaibhav> : I think as we can have a super class variable reference which can refer to a subclass object. So, to achieve this we can't restrict the access since whatever method is visible in superclass, it should not be hidden in subclass.
ii. We can't have return type other than the same return type or subclass of the return type defined in the superclass. Why it is so?
<Vaibhav> : Same as given above. Whatever return type we are expecting for a super class method, similar hierarchy must be followed by subclass.
iii. We can add more exceptions in the subclass. I am wondering why we can add more exceptions in throws clause in the subclass which were not defined in the superclass?
<Vaibhav> : If above two points are correct, then, I wonder why it is different in case of exceptions i.e. if super class doesn't declare any exception then how can we add exceptions in subclasses. We will be having a super class variable referring to a subclass object then how will it handle additional exceptions?
Campbell Ritchie wrote:I beleive that an overriding method should never be able to throw an Exception which is not a subclass of an Exception thrown by its overridden version. The reason is the same as why you can't override a public method with protected access.