Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1

Chapter 5 ■ a Java primer: introduCtion to Java ConCepts and prinCiples


(APIs), the public keyword is most often used in open source programming Java platforms or packages that
are used to create custom applications, including games.
It is important to note that if a public class that you are trying to access and utilize exists in another
package other than your own package (in our case, your own package will be named boardgame), then
you will need to use the Java import keyword to create an import statement to be able to utilize that public
class. This is why, by the end of this book, you will have dozens of import statements at the top of your
JavaFXGame.java class. You will be leveraging preexisting Java and JavaFX classes in code libraries, which
have already been coded, tested, refined, and made public, by using a public access control modifier
keyword so that you can create pro Java 9 games and IoT apps that leverage Java APIs.
Because of class inheritance in Java, all of the public methods and public variables inside a public
class will be inherited by the subclasses of that class (which, once it is subclassed, becomes a superclass).
You can see an example of a public access control modifier keyword in front of the Invincibagel class
keyword, as shown in Figure 5-2.


Java Protected Modifier: Variables and Methods Allow Access by Subclasses


The Java protected access modifier keyword can be used by data fields (variables and constants) and by
methods, including constructor methods, but cannot be used by classes or interfaces. We will be covering
Java interfaces later in this chapter. The protected keyword allows variables, methods, and constructors in a
superclass to be accessed only by subclasses of that superclass in other packages (such as your boardgame
package) or by any class within the same package as the class containing those protected members (Java
constructs). Using this access control modifier is like putting a lock on the original Java code; to use the
original code (much less to add to it and thus modify its intended usage), you are forced to extend, or
subclass, the protected class, and then you can override its methods.
This access modifier keyword therefore essentially protects methods or variables in a class that is
intended to be (is hoped to be) used as a superclass by being subclassed (extended) by other developers.
Unless you own this package, which these protected Java constructs are defined inside (which you don’t),
you must extend this superclass and create your own subclass implementation in order to be able to utilize
these protected methods and variables.
You might be wondering when would one want to do this and protect Java code structures like this?
When you are designing a larger project, such as the Android operating system API, for instance, you will
often want to have the highest-level methods and variables not be used directly, right out of the class, or
directly from within that class.
In this situation, where others are using your code structures, you would rather that your original Java
code be used within a separately defined, developer-coded subclass structure. This “isolates” the superclass
code so that it will remain directly untouched, in a sense, guaranteeing that the original methods, fields, and
intent are maintained as they were originally intended by the Java code author or authors (package owner
or owners), protected from others’ modifications. This ensures that your API and its superclasses remain, ad
infinitum, as a “blueprint” for other Java 9 developers to utilize to create their own (Android, JavaFX, etc.)
games, business utilities, and IoT applications.
You can achieve this direct use prevention by protecting methods and variable constructs from being
used directly so that they become only a blueprint for more detailed implementations in other classes and
are not able to be used directly. Essentially, protecting a method or variable turns it into a blueprint, or
“implementation road map.”


Java Private Modifier: Fields, Methods, or Constructors Allowed Local Access


The Java private access control modifier keyword can be used by data fields (variables or constants) and by
methods, including constructor methods and interfaces, but cannot be used by classes. We will be covering
Java interfaces later in this chapter. The private access control keyword allows variables, methods, and

Free download pdf