A (175)

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

The Foundation of OOP: The Java Object


The foundation of OOP is, no surprises here, the object itself, because everything in Java is an object.


Objects in OOP languages are similar to the objects that you see around you every day, except Java
objects are virtual, and are not tangible, since computers use zeroes and ones (binary) to represent
things. Just like tangible real-world objects, Java objects have characteristics, called states, and
things that they do, called behaviors.


One way to think about this distinction is that Java objects are nouns, or things that exist in
and of themselves, whereas their behaviors are like verbs, or things that these nouns can do.
As an example, let’s consider that very popular object in all of our lives: the automobile. Some
characteristics, or states, of the car might be as follows:


   Color (candy apple red)
 Direction (N, S, E, or W)
 Speed (15 miles per hour)
 Engine type (gas, diesel, hydrogen, propane, or electric)
 Gear setting (1, 2, 3, 4, or 5)
 Drivetrain type (2WD or 4WD)

The following are some things that you can do with a car, that is, the car’s behaviors:


   Accelerate
 Shift gears
 Apply the brake
 Turn the wheels
 Turn on the stereo
 Use the headlights
 Use the turn signals

You get the idea. Now stop daydreaming about your new car, and let’s get back down to learning
about objects!


Figure 5-1 shows a simple “Anatomy of a Car Object” diagram of the Java object structure, using a
car as an example. It shows the characteristics, or attributes, of the car, that are central to defining
the car object, and the behaviors that the car object can perform. These attributes and behaviors
serve to define the car to the outside world, just like your application’s objects will for your Android
application.

Free download pdf