Game Development Reference
In-Depth Information
archy terminology. A class will always subclass a superclass using a Java extends
keyword. If a class does not extend a given superclass in this way, then it automatically
extends the Java masterclass: java.lang.Object . This is so that every class in Java can
create an object by implementing a constructor method.
Using a Java extends keyword tells the compiler that you want the superclass's
capabilities and functionality added (extended) to your class, which, once it uses this
extends keyword, becomes a subclass. A subclass extends the core functionality that is
provided by the superclass. To extend your class definition to include a superclass, you
add to (or extend , no pun intended) your existing class declaration, using the following
format:
<modifier keywords> class <your custom classname> extends
<superclass>
When you extend a superclass with your class, which is now a subclass of that su-
perclass, you can use all the superclass's features (nested classes, inner classes, meth-
ods, variables, constants) in your subclass, without having them all explicitly written
(coded) in the body of your class, which would be redundant (and disorganized).
Note If any of the data fields or methods in the superclass that you are extending (or,
if you prefer, subclassing) have been declared using the private access control keyword,
those variables (or constants) and methods are reserved for use only by (or within) that
superclass, and thus will not be accessible to your subclass. The same rules apply to
nested and inner classes; these class structures cannot use any code declared as private
in the Java constructs that contain them (or that are above them, if you will).
The body of your class is coded inside the curly braces (see Figure 3-2 , outermost
red box), which follow your class ( and javafx.application.Application superclass , in
this case) declaration. This is why you learned about Java syntax first, and you are
building on that with the class declaration and the Java syntax that holds the class
definition (variables, constants, methods, constructor, nested classes) constructs.
As you can see in the figure, the InvinciBagel class extends an Application super-
class from the JavaFX package. The inheritance diagram (a tool I will be using
throughout the topic to show you where things come from in the overall Java and
JavaFX API schemas) for your current superclass-to-subclass hierarchy will therefore
look like this:
Search WWH ::




Custom Search