A (175)

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

It’s important to remember that you can use both Java program logic, as well as XML markup, to
define objects for Android applications, as you learned in the previous chapter when you learned
about object inflation. You have already defined a TextView user interface object, having several
characteristics, in Chapter 4 using XML markup (refer to Figure 4-1, to see the XML tag and
parameters defining the TextView user interface (UI) object definition).


OOP Terminology: Variables, Methods, Constants


Next, let’s cover some of the technical terminology used in conjunction with Java objects. First,
objects have data fields to hold variable data, constants to hold fixed data, and methods to define
behaviors, as follows:


   Data fields that hold a Java object’s states, or things that can change about an
object’s characteristics over time, are called “variables.” Using the car example,
the direction you’re driving in, the gear that you’re driving in, and the speed
you’re driving at, are all variables.
 Data fields that hold an object’s attributes, or things that do not change
regarding an object’s characteristics over time, are called “constants.” Using
the car example, the candy apple red paint job on the car would be a constant,
as is the car’s engine type (unless you own an automotive paint and body shop,
or are an auto mechanic, that is).
 Methods are programming logic, or program code routines, that operate on,
and change, the object’s internal data fields or states. Methods will also allow
other Java objects that are external to the object itself to communicate with
that object, as long as the method is declared to be “public.” We will be getting
into methods in greater detail a bit later on in the chapter, so I won’t get into too
deep a level of detail regarding how methods work here.

One of the key concepts of OOP is data encapsulation. In Java, data encapsulation is implemented
by only allowing a Java object’s variable data fields to be modified directly through that same Java
object’s internal (private) methods. This allows a Java object to be self-sufficient, or “encapsulated.“


Using the car example, in order to turn the car, you would use the .turnWheels( ) method, shown
in Figure 5-1 on the bottom-right of the diagram. This method would be comprised of Java
programming logic that would correctly position the wheels of the car, ultimately causing it to move
in the desired direction. You would not see the details of how the object’s wheels turned, because of
encapsulation. That detail is left to the private, internal functionality in the object.


Note Notice the empty parentheses I am using after my method names in the text. These are always
used when writing about a method, so that the reader knows that the author is referencing a Java method.
Additionally, since method calls are invoked using dot notation, I also preface the method names with a dot,
further reinforcing that this is a method call, so that you can visualize it. You will see this naming convention
used throughout the rest of this book.
Free download pdf