content









Content










2.2
What
are Objects?




2.2.7
Encapsulation




Encapsulation hides details of an
object. It makes objects robust by protecting data from being accessed
and changed by other objects. Users know the name of the methods
within the object, but not how the methods work. In most cases, there
is no need for others to know how it works. For example, most people
do not care how the gas gauge of a car knows that the gas tank is
almost empty. However, they do know that when the needle of the gas
gauge hovers near the "E", it is time to fill up.
Encapsulation also means 'to
package together'. An object always has its data and methods
packaged together. For example, a Customer object cannot access the DateDue attribute of the VideoRented object and set its value. The
Customer object will need to send a message to the VideoRented object
using its calculateDateDue method.
OOP languages implement
encapsulation through special keywords that a programmer can use in
defining classes. For example, the Java language uses keywords private,
public, and
protected to identify different levels of
encapsulation of an object. These identifiers are often described as
access modifiers. Access modifiers define the level of access to the
class, data, or method. A properly encapsulated object will have all
of its member data identified as
private and most of the accessors and
mutators identified as public.
Use the activity to identify the level
of access allowed by each access modifier by dragging the appropriate
term to its corresponding definition.
The beauty of objects is that they can
be used even if how they work inside is unknown. Objects are "little
black boxes of functionality". The following is one answer
to the question "What is a black box?" and includes a detailed
description.
A black box is an engineering term that
works like this. First, something is dropped into a black box. Then, wait
while this thing is magically transformed inside the black box.
Finally, a new transformed thing is received back from the black box.
The beauty of a black box is that the only thing that needs to be known
is how to drop something into it and what to expect on the other side.
Knowledge about the inner-workings are not necessary.
 


 











Wyszukiwarka

Podobne podstrony:
content
content
content
content
content
content
content
content
content
function domnode get content
content
content
content
content
content
content

więcej podobnych podstron