Sub topic 9
Access Rules for Overridden Methods
What overriding means
A superclass is a class that another class builds on. A subclass is the class that builds on it. The subclass inherits, or receives, accessible methods from the superclass.
Overriding happens when a subclass provides its own version of an inherited method. The new method has the same name and parameter types as the superclass method. Its return type must also be compatible.
An access modifier controls where code can use a class member, such as a method. When you override a method, you can give it the same access or wider access. You cannot make it less accessible.
Why? Code that can call a superclass method must still be able to call that method when the object is a subclass object.
Choose the access level
A package is a group of Java classes. A method with no access modifier is package-private, which means it is accessible within its package.
| Superclass method access | Access allowed for the override |
|---|---|
| `public` | `public` |
| `protected` | `protected` or `public` |
| Package-private, when inherited in the same package | Package-private, `protected`, or `public` |
| `private` | Cannot be overridden |
For example, if a superclass method is public, an override cannot be protected or private. Java reports a compile error: the program cannot be translated into runnable code until the error is fixed.
A private method is not inherited for overriding. A subclass may declare a method with the same name and parameters, but that method is not an override of the private method.
Work through an example
Save this program as Main.java:
class Notification {
public void send() {
System.out.println("Sending a notification");
}
protected void prepare() {
System.out.println("Preparing a notification");
}
}
class EmailNotification extends Notification {
@Override
public void send() {
prepare();
System.out.println("Sending an email");
}
@Override
public void prepare() {
System.out.println("Preparing an email");
}
}
public class Main {
public static void main(String[] args) {
Notification message = new EmailNotification();
message.send();
}
}Run it from the folder containing the file:
javac Main.java
java MainThe program prints “Preparing an email” and then “Sending an email”, on separate lines.
Notice the access choices:
Notification.send()ispublic, so the overridingsend()must also bepublic.Notification.prepare()isprotected, so its override may beprotectedorpublic. The example makes itpublic.
The variable message is declared as a Notification, but the object is an EmailNotification. Java runs the subclass version of send() for that object. That version then calls the subclass version of prepare().
Making an override public does not change the access modifier on the superclass declaration. When code uses a variable declared as the superclass type, access is checked using that type. For example, code outside the package cannot assume it may call prepare() through a Notification variable just because the subclass override is public.
Let `@Override` catch mistakes
@Override is an annotation: a marker that gives Java extra information about a declaration. It tells Java that you intend to override a method. Java checks that the method actually does so.
Use it on each method you intend to override. It can catch a misspelled method name or wrong parameter types. It also helps reveal when a superclass method is not available to override.
For example, a package-private method is not inherited by a direct subclass in a different package. If that subclass declares a method with the same name and parameters, it does not override that method. If no other inherited method matches, @Override causes a compile error.
Two other cases are not overrides:
- A
finalmethod cannot be overridden. - A
staticmethod belongs to the class rather than to each object. A subclass method with the same signature hides it instead of overriding it.
Practice
Exercise
A superclass has a protected method. Can its subclass override it with a private method? Which access levels can the override use?
Show answer
No. Private access is narrower than protected access. The override can be protected or public.
Exercise
A direct subclass in a different package declares a method with the same name and parameters as a package-private superclass method. Does it override that method?
Show answer
No. The package-private method is not inherited by that subclass, so the new method is not its override.