Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
It doesn't override, but it hides the method Base.amethode() (just outcomment the method in the sub class). And it looks like the rules are the same for hiding as for overriding. So it isn't allowed to use wider exceptions in the subclass.
So if you swap the exceptions all is fine, like in the following code:
Ryan Anderson wrote:I don't understand why the static method is not inherited. If the same rules apply to hiding and inheriting then why not just say that the static method is inherited.
I know that it says in K&B that "a static method cannot be overridden" but if these same rules apply then aren't we overriding the method in the subclass when following the rules?
Why are static methods not inherited? There is no polymorphisus, right. But AFAIK they are inherited. Remember, there is no hiding without inheritance. What should be hidden, if there is nothing to hide?
From sun (sun.com):
You can use the inherited members as is, replace them, hide them, or supplement them with new members
Try this code and you see it can access the static Base.amethod():
So it is clear that super class static methods are inherited. Also one more point is that if we override a static method then compiler allows us if and only if we mark it also as static. If we override a super class protected method by a sub class default method then it says 'attempting to assign weaker access privileges'. We are decreasing the ability of the method. So if we override a static method with non-static one then we are decreasing the ability of the method to be able to be called without the need of an instance. Make the sub class static method private in the code and compiler says the same thing which it says for a non-static method override. So rules are same for legal overrides related to access modifiers and exceptions but just one more thing is non-acess modifier, that is static.
But check the following :-
I tried to analyse on my own. Correct me if I am wrong.