I have the following code:
public class Library {
public void myFunction() {
// do something
}
}
public class Book extends Library{
protected void myFunction() { // Error here
// do something
}
}
The above code has error because the Book class is trying to override a function of the supper class with a weaker access modifier. I know this is the rule of Java. But I am curious why is that? What the problem it might cause?
Consider, from an entirely different package:
The issue really comes down to
myFunctionbeing virtual; the method invoked depends on the runtime-type and not the static type of the expression. (I believe C# would actually allow this for a non-virtual method with thenewmodifier.)