This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
when we override why cant we grant weaker access to overridden methods...i need ans in object oriented way....if weaker access to granted in subclass what are all possible problems that could come up..ans with example would be more elaborative
Because there is an is a relationship between the superclass and the subclass (the Liskov substitution principle that Rob mentioned). If this were allowed, there would be a backdoor through which you could call methods that should not be accessible.
You can assign an instance of a subclass to a variable that has the type of the superclass:
The call in line 17 would be possible, because method() is public in class Super. But it should not be allowed, because obj is really referring to an instance of Sub, and in that class, the method is protected! So, there is a backdoor through which you could now suddenly call a method in class Sub that should not be accessible from the outside.
To prevent this, it's not allowed to give an overriding method more restricted access than the method it is overriding.