Here are a few fun facts about Java modifiers used in Java programs like designing Android Apps. The word final has many uses in Java programs. In addition to having final variables, you can have these elements:
Final class: If you declare a class to be final, no one (not even you) can extend it.
Final method: If you declare a method to be final, no one (not even you) can override it.
These figures put these rules into perspective. They show you can't extend the Stuff class, because the Stuff class is final. And, you can't override the Stuff class's increment method because that increment method is final.
You can apply Java's protected keyword to a class's members. This protected keyword has always seemed a bit strange to some. In common English usage, when your possessions are "protected," your possessions aren't as available as they'd normally be.
But in Java, when you preface a field or a method with the protected keyword, you make that field or method a bit more available than it would be by default, as shown in the figure.
A default member of a class (a member whose declaration doesn't contain the words public, private, or protected) can be used by any code inside the same package as that class.
The same thing is true about a protected class member. But in addition, a protected member is inherited outside the class's package by any subclass of the class containing that protected member.
Huh? What does that last sentence mean, about protected members? To make things concrete, this figure shows you the carefree existence in which two classes are in the same package. With both Stuff and MyStuff in the same package, the MyStuff class inherits the Stuff class's default value variable and the Stuff class's default increment method.
If you move the Stuff class to a different package, MyStuff no longer inherits the Stuff class's default value variable or the Stuff class's default increment method, as shown here.
But if you turn value into a protected variable and you turn increment into a protected method, the MyStuff class again inherits its parent class's value variable and increment method, as shown here.
Notice one more detail in the figures. The MyStuff class's increment method was changed from default to public. This was done to avoid seeing an interesting little error message. You can't override a method with another method whose access is more restrictive than the original method. In other words, you can't override a public method with a private method. You can't even override a public method with a default method.
Java's default access is more restrictive than protected access, as shown in this figure. So you can't override a protected method with a default method.
In this example, the whole issue was avoided by making public the MyStuff class's increment method. That way, the increment method with the least restrictive kind of access was overridden.