What must be true for the access modifier of a subclass when it is overriding a method in the superclass?
Answer:
The overriding method cannot have a more restrictive access modifier than the method being overridden.
You are allowed to override a method as long as the overiding method's access modifier is not more restrictive than the original one. This means you can do the following:
Allowed:
class Parent {
public void aMethod() {
}
}
class Child extends Parent {
public void aMethod() {
}
}
You are not allowed to do the following:
Not Allowed:
class Parent {
public void aMethod() {
}
}
class Child extends Parent {
protected void aMethod() {
}
}
In the above example, protected in the subclass is more restrictive than public in the superclass.