How to Add Buttons and Text to Your JavaFX Project
How to Retrieve Items from a LinkedList in Java
Understanding the Structure of Java Classes

Java's Modifiers

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.

Trying to extend a final class.
Trying to extend a final class.
Trying to override a final method.
Trying to override a final method.

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.

Access modes for fields and methods.
Access modes for fields and methods.

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.

Two classes in the same package.
Two classes in the same package.

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.

Classes in different packages.
Classes in different packages.

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.

Using the protected modifier.
Using the protected modifier.

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.

image6.jpg

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.

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
How to Compile a Java Program in TextPad
Shadowed Classes or Variables in Java
How to Use the JScrollPane Class to Create Text Areas with Scroll Bars in Java
How to Use Class Declarations in Java
How to Create a Bare-Bones JavaFX Project
Advertisement

Inside Dummies.com