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 />

■ ■Note It is possible to use, instead of the <strong>Java</strong> import keyword, the fully qualified class name, that is, to preface the<br />

class name with the package name, right inside your <strong>Java</strong> code. Convention dictates using the import statement; however,<br />

line 20 in Figure 3-2 could be written as javafx.scene.control.Button btn = new javafx.scene.control.<br />

Button(); if you wanted to buck standard <strong>Java</strong> programming convention.<br />

<strong>Java</strong> Classes: Logical <strong>Java</strong> Constructs to Build On<br />

The next logical <strong>Java</strong> programming construct beneath the package level is the <strong>Java</strong> class level, as you saw in the import<br />

statement, which references both the package that contains the class and a class itself. Just as a package organizes all<br />

the related classes, so too a class organizes all its related methods, variables, and constants and, sometimes, other<br />

nested classes.<br />

Thus, the <strong>Java</strong> class is used to organize your <strong>Java</strong> code at the next logical level of functional organization, and so<br />

your class will contain <strong>Java</strong> code constructs that add functionality to your application. These may include methods,<br />

variables, constants, nested classes, or inner classes.<br />

<strong>Java</strong> classes can also be used to create <strong>Java</strong> objects. <strong>Java</strong> objects are constructed, using your <strong>Java</strong> class, and have<br />

the same name as the <strong>Java</strong> class and as that class’s constructor method.<br />

As you saw in Figure 3-2, you declare your class, using a <strong>Java</strong> class keyword, along with a name for your class. You<br />

can also preface the declaration with <strong>Java</strong> modifier keywords, which you will be studying later in this chapter (see the<br />

section “<strong>Java</strong> Modifier Keywords: Access Control and More”). <strong>Java</strong> modifier keywords are always placed before (or in<br />

front of) the <strong>Java</strong> class keyword, using the following format:<br />

class <br />

One of the powerful features of <strong>Java</strong> classes is that they can be used to modularize your <strong>Java</strong> game code so<br />

that your core game application features can be a part of a high-level class that can be subclassed to create more<br />

specialized versions of that class. Once a class has been subclassed, it becomes a superclass, in <strong>Java</strong> class hierarchy<br />

terminology. A class will always subclass a superclass using a <strong>Java</strong> extends keyword. If a class does not extend a given<br />

superclass in this way, then it automatically extends the <strong>Java</strong> masterclass: java.lang.Object. This is so that every class<br />

in <strong>Java</strong> can create an object by implementing a constructor method.<br />

Using a <strong>Java</strong> extends keyword tells the compiler that you want the superclass’s capabilities and functionality<br />

added (extended) to your class, which, once it uses this extends keyword, becomes a subclass. A subclass extends the<br />

core functionality that is provided by the superclass. To extend your class definition to include a superclass, you add to<br />

(or extend, no pun intended) your existing class declaration, using the following format:<br />

class extends <br />

When you extend a superclass with your class, which is now a subclass of that superclass, you can use all the<br />

superclass’s features (nested classes, inner classes, methods, variables, constants) in your subclass, without having<br />

them all explicitly written (coded) in the body of your class, which would be redundant (and disorganized).<br />

48<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!