A (175)

(Tuis.) #1
CHAPTER 5: Introduction to Java: Objects, Methods, Classes, and Interfaces 147

Be sure to use good programming practices and refer to documentation for your superclass’s fields
and methods within each subclass that uses the super keyword to reference these superclass
programming infrastructures in one way or another. The Java class documentation should let users
(developers) of your superclass know which of your superclass fields and methods are “public” and
are available for use, since these do not explicitly appear in the Java code for the subclass, as only
incremental code (methods and variables) appear in the subclass code.


The Interface: Defining a Class Usage Pattern


In many Java applications, as well as in the Android APIs, Java classes must conform to a certain
usage pattern.


There is a specialized Java construct called an interface that can be implemented so that
application developers will know exactly how to utilize the Java classes implementing an interface,
as well as the methods required for proper implementation of the class. Implementing an interface will
thus allow your class to become more “conformant” regarding those behaviors that your class offers
for use.


Interfaces in essence are forming a “programming contract” between your class and the rest of the
development world. By implementing a Java interface, the Java compiler can enforce this contract
at build time. 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 programming framework like
Android that is utilized by other 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 can be used like a roadmap, 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 inter-working, inter-dependent collective programming structure, guaranteeing that any
individual function needed to implement that functional collective does not get inadvertently left out.


This public interface that the class “presents” to other developers who are using the Java language
and Android platform makes using that class more predictable, and allows developers to safely
use that class in programming structures and objectives where a class of that particular end-usage
pattern is suitable for their implementation.


In other words, a public interface is an implementation roadmap that tells your application what
that class needs to do and how to implement it without your application needing to test that class’s
functional capabilities.


In Java terms, making a class conform to a usage pattern is done by implementing an interface.
The following is an ICar interface, which forces all cars to implement all of the methods that are
defined in this interface.


These methods must be implemented (exist) even if they are not utilized (that is, no code exists
inside the curly braces). This also guarantees that the rest of the Java application knows that each
Car object can perform all of these actions or behaviors, because implementing the ICar interface
defines this public interface for all of the Car objects that implement the ICar interface.

Free download pdf