Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1

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


the subclass from the superclass by using a Java extends keyword inside of the class declaration. The Java
class construct would thus look just like this:


class Suv extends Car {
void applyBrake (int brakingFactor) {
super.applyBrake(brakingFactor);
speed = speed - brakingFactor;
}
}


This extends the Suv Object to have access to, essentially to contain, all of the data fields and methods
that the Car Object features. This allows the developer to only have to focus on the new, or different, data
fields and methods that relate to the differentiation of the Suv Object from the regular or “master” Car Object
definition.
To refer to one of the superclass’s methods from within the subclass that you are coding, you can use the
Java super keyword. For example, in the new Suv class you may want to use the Car superclass .applyBrake()
method and then apply some additional functionality to the brake that is specific to Suv. You call the
Car object’s .applyBrake() method by using super.applyBrake() in the Java code. The Java code shown
earlier will add additional functionality to the Car object’s .applyBrake() method, inside of the Suv Object
.applyBrake() method by using this super keyword to access the Car Object’s .applyBrake() method and then
add in additional logic to make the brakingFactor apply twice. This serves to give the Suv object twice the
braking power that a standard car would have, which an SUV would need.
The reason this Java code doubles the SUV’s braking power is because the Suv object’s .applyBrake()
method first calls the Car object’s .applyBrake() method from the Car superclass using a super.applyBrake
(brakingFactor); line of Java code in the Suv subclass’s .applyBrake() method. The line of Java code that
comes next increments the speed variable by applying brakingFactor a second time, making your SUV
object’s brakes twice as powerful.


The Java Interface: Defining the Class Usage Pattern


In many Java applications, Java classes must conform to a certain usage pattern. There is a specialized Java
construct that is called an interface that can be implemented so that application developers will know
exactly how to implement those Java classes, including alerting developers that methods are required for a
proper implementation of the class. Defining an interface will allow your class to inform other developers
using that class that behaviors (which Java methods) for your class must be implemented in order to
correctly utilize your Java class’s infrastructure.
Interfaces in essence prescribe a programming contract between the class and the rest of the
development community. By implementing a Java interface, a contract can be enforced at build time by the
Java compiler. If a class “claims” to implement a public interface, all of the methods that are “defined” by that
Java interface definition must appear in the source code for the class that implements that interface, before
that class will successfully compile.
Interfaces are especially useful when working within a complex, Java-based programming framework,
such as Android uses, that is utilized by developers who build applications on the Java classes that the
Google Android OS developer team members have written specifically for that purpose. A Java interface
should be used like a road map, showing developers how to best implement, and utilize, the Java code
structure that is provided by that Java class within another Java programming structure.
Basically, a Java interface guarantees that all methods in a given class will get implemented together
as an interworking, interdependent, collective programming structure, guaranteeing that any individual
function needed to implement that functional collective does not get inadvertently left out. This public
interface that a class “presents” to other developers who are using the Java language makes using that class

Free download pdf