136 CHAPTER 5: Introduction to Java: Objects, Methods, Classes, and Interfaces
With data encapsulation, you can build and test each object that is part of a larger object construct
individually, without requiring data to be accessed from other objects, or modules, of your
application. External data access can translate into “bugs,” so encapsulation helps when developing
complicated, large-scale applications.
Without data encapsulation, developers on your team could simply access any part of your object data
and use it however they pleased. This could introduce bugs affecting the methods you have perfected
to manipulate your object and to provide your encapsulated solution. So data encapsulation promotes
the core concept in OOP of modularity. Once an object is created and tested, other objects can use it
without worrying about its integrity.
Data encapsulation. thus allows code re-use, so programmers can develop “libraries” of useful
objects that do not need to be rewritten, or retested, by other programmers. You can see how this
might save developers time and money, by structuring only the work that needs to be done, and
avoiding redundant work processes related to testing multiple code modules in conjunction with one
another. Data encapsulation will also allow developers to hide the data and the internal logic of a
Java object, if desired.
Finally, Java objects make debugging easier, because you can add or remove them modularly
during testing to ascertain where bugs are located within the overall code. In the car object example,
the attributes of our car are encapsulated inside of the car object, and can thus be changed only
via the methods that surround them in each diagram. For instance, one would use a .shiftGears( )
method to change the Gears=1 field variable to Gears=2.
Java Constructs: Create Your Own Objects
In the next several sections of this chapter, you will learn about the primary Java programming
“constructs,” or code structures, that developers can create to be able to define their own custom
Java objects. These custom objects will have their own attributes (constants), characteristics
(variables), behaviors (methods), accessibility (access control modifiers), procreation (constructors),
and rules of engagement (interfaces).
This is in large part accomplished using the top-level structure in Java, which is called a Java class.
For this reason, we will start learning about classes first, since all of the other structures in Java are
either created (nested) inside of the class or relate to its usage and implementation in some way.
The Java Class: Java Code Structure Container
In real life, there is seldom just one single type or kind of object. Usually, there is a large number of
different object types and variations. For instance, for a car object, there are many different manufacturers,
sizes, shapes, seating capacities, engine types, fuel types, transmission types, drive-train types,
and so on.
In Java SE, you write something called a “class” that defines what your object can do (its methods),
and what data fields it will possess. Once the class has been coded in Java, you can then create
an “instance” of an object that you wish to use, by referencing the class definition. In architectural
terms, the class is a kind of blueprint as to what the object will be structured like, including what
states it will contain (its variables), and what tasks it can perform (the methods that it has).