28.04.2019 Views

[JAVA][Beginning Java 8 Games Development]

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 3 ■ A <strong>Java</strong> 8 Primer: An Introduction to <strong>Java</strong> 8 Concepts and Principles<br />

As you can see at the bottom of Figure 3-2, for the .main() method, created by NetBeans for your InvinciBagel<br />

class definition, which uses the public modifier, you can use more than one <strong>Java</strong> modifier keyword. The .main()<br />

method first uses a public modifier keyword, which is an access control modifier keyword, and then a static modifier<br />

keyword, which is a nonaccess control modifier keyword.<br />

Access Control Modifiers: Public, Protected, Private, Package Private<br />

Let’s cover access control modifiers first, because they are declared first, before nonaccess modifier keywords or<br />

return type keywords, and because they are easier to understand conceptually. There are four access control modifier<br />

levels that are applied to any <strong>Java</strong> code structure. If you do not declare an access control modifier keyword, a default<br />

access control level of package private will be applied to that code structure, which allows it to be visible, and thus<br />

usable, to any <strong>Java</strong> programming structure inside your <strong>Java</strong> package (in this case, invincibagel).<br />

The other three access control modifier levels have their own access control modifier keywords, including public,<br />

private, and protected. These are somewhat aptly named for what they do, so you probably have a good idea of how<br />

to apply them to either share your code publicly or protect it from public usage, but let’s cover each one in detail<br />

here, just to make sure, as access (security) is an important issue these days, inside your code as well as in the outside<br />

world. I will start with the least amount of access control first!<br />

<strong>Java</strong>’s Public Modifier: Allowing Access by the Public to <strong>Java</strong> Program Constructs<br />

The <strong>Java</strong> public access modifier keyword can be used by classes, methods, constructors, data fields (variables and<br />

constants), and interfaces. If you declare something public, it can be accessed by the public! This means that it can be<br />

imported and used by any other class, in any other package, in the entire world. Essentially, your code can be used in<br />

any software that is created using the <strong>Java</strong> programming language. As you will see in the classes that you use from the<br />

<strong>Java</strong> or <strong>Java</strong>FX programming platforms (APIs), the public keyword is most often used in open-source programming<br />

<strong>Java</strong> platforms or packages that are employed to create custom applications, such as games.<br />

It is important to note that if a public class that you are trying to access and use exists in a package other than your<br />

own (in your case, invincibagel), then the <strong>Java</strong> programming convention is to use the <strong>Java</strong> import keyword to create an<br />

import statement that allows use of that public class. This is why, by the time you reach the end of this book, you will<br />

have dozens of import statements at the top of your InvinciBagel.java class, as you will be leveraging preexisting <strong>Java</strong><br />

and <strong>Java</strong>FX classes in code libraries that have already been coded, tested, refined, and made public, using the public<br />

access control modifier keyword, so that you can create <strong>Java</strong> 8 games with them to your heart’s content!<br />

Owing to the concept of class inheritance in <strong>Java</strong>, all the public methods and public variables inside a public<br />

class will be inherited by the subclasses of that class (which, once it is subclassed, becomes a superclass). Figure 3-2<br />

offers an example of a public access control modifier keyword, in front of the InvinciBagel class keyword.<br />

<strong>Java</strong>’s Protected Modifier: Variables and Methods Allow Access by Subclass<br />

The <strong>Java</strong> protected access modifier keyword can be used by data fields (variables and constants) and by methods,<br />

including constructor methods, but cannot be used by classes or interfaces. The protected keyword allows variables,<br />

methods, and constructors in a superclass to be accessed only by subclasses of that superclass in other packages (such<br />

as the invincibagel package) or by any class within the same package as the class containing those protected members<br />

(<strong>Java</strong> constructs).<br />

This access modifier keyword essentially protects methods and variables in a class that is intended to be (hoped<br />

to be used as) a superclass by being subclassed (extended) by other developers. Unless you own the package that<br />

contains these protected <strong>Java</strong> constructs (which you do not), you must extend the superclass and create your own<br />

subclass from that superclass to be able to use the protected methods.<br />

57

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!