Java Reference
In-Depth Information
You have seen two major benefits of data abstraction in this section.
It lets you extend the programming language by letting you define new data types. The new
data types you create depend on the application domain. For example, for a banking system,
Person , Currency , and Account may be good choices for new data types whereas for an auto
insurance application, Person , Vehicle , and Claim may be good choices. The operations
included in a new data type depend on the need of the application.
The data type created using data abstraction may change the representation of the data
without affecting the client code using the data type.
Encapsulation and Information Hiding
The term encapsulation is used to mean two different things: a process or an entity. As a process, it is an act of
bundling one or more items into a container. The container could be physical or logical. As an entity, it is a container
that holds one or more items.
Programming languages support encapsulations in many ways. A procedure is an encapsulation of steps
to perform a task; an array is an encapsulation of several elements of the same type, etc. In object-oriented
programming, encapsulation is bundling of data and operations on the data into an entity called a class.
Java supports encapsulation in various ways.
It lets you bundle data and methods that operate on the data in an entity called a class.
It lets you bundle one or more logically related classes in an entity called a package. A package
in Java is a logical collection of one or more related classes. A package creates a new naming
scope in which all classes must have unique names. Two classes may have the same name in
Java as long as they are bundled (or encapsulated) in two different packages.
It lets you bundle one or more related classes in an entity called a compilation unit. All classes
in a compilation unit can be compiled separately from other compilation units.
While discussing the concepts of object-oriented programming, the two terms, encapsulation and information
hiding, are often used interchangeably. However, they are different concepts in object-oriented programming, and
they should not be used interchangeably as such. Encapsulation is simply the bundling of items together into one
entity. Information hiding is the process of hiding implementation details that are likely to change. Encapsulation is
not concerned with whether the items that are bundled in an entity are hidden from other modules in the application
or not. What should be hidden (or ignored) and what should not be hidden is the concern of abstraction. Abstraction
is only concerned about which item should be hidden. Abstraction is not concerned about how the item should be
hidden. Information hiding is concerned with how an item is hidden. Encapsulation, abstraction, and information
hiding are three separate concepts. They are very closely related, though. One concept facilitates the workings of the
others. It is important to understand the subtle differences in roles they play in object-oriented programming.
It is possible to use encapsulation with or without hiding any information. For example, the Person class in
Listing 1-1 shows an example of encapsulation and information hiding. The data elements ( name and gender )
and methods ( getName() , setName() , and getGender() ) are bundled together in a class called Person . This is
encapsulation. In other words, the Person class is an encapsulation of the data elements name and gender , plus the
methods getName() , setName() , and getGender() . The same Person class uses information hiding by hiding the data
elements from the outside world. Note that name and gender data elements use the Java keyword private , which
essentially hides them from the outside world. Listing 1-6 shows the code for a Person2 class.
 
Search WWH ::




Custom Search