EEI3262 Unit 2 Session 6
EEI3262 Unit 2 Session 6
Session 6
Encapsulation
Contents
Introduction, p 84
Encapsulation, p 84
Summary, p82
Learning Outcomes, p88
Review Questions, p88
Introduction
As you already studied, there are four object-oriented concepts, inheritance,
encapsulation, polymorphism and abstraction. The earlier session described
objects and classes and you will be learning Encapsulation in this session.
Encapsulation is a method of packing data and sometimes functions into one
component. This allows certain information to remain private and it
promotes maintenance, as code changes can be made independently. It
increases usability by keeping data private and providing public, well-
defined service methods. Therefore, the role of the object becomes clear to
other objects. Let’s study the use of encapsulation as an object-oriented
concept in the next section.
Encapsulation
Encapsulation is used mainly for two reasons. First, it is the process by
which data is bundled together with operators. Secondly, it is the hiding or
limiting of access to data to ensure security. Encapsulation means that the
internal representation of an object is generally hidden from view outside of
the object's definition. Typically, only the object's own methods can directly
inspect or manipulate its fields. Some languages
like Smalltalk and Ruby only allow access via object methods, but most
others (e.g. C++, C#, Delphi or Java) offer the programmer a degree of
control over what is hidden, typically via keywords like public and private
Consider the example given below.
84
Session 6 : Encapsulation
Here the fields are defined as public. Anybody who is using your class can
access and change the fields you defined as they want. This might lead to
confusions such as ending up with negative values for age and causing other
problems within the application. You can limit the access to these fields by
changing the access specifier to private. Let’s rewrite the code as given
below.
Since we have defined the fields private, we have to provide a way to others
to access these fields. To do that we can write separate methods to assign
data(set methods) and to retrieve data(get methods) to these fields.
The modified code with these get and set methods are given below.
public class EncapTest {
private String name;
private String idNum;
private int age;
public int getAge() {
return age;
}
85
Session 6 : Encapsulation
Usually the accessor methods have public visibility while the field being
encapsulated is given private visibility. This allows a programmer to restrict
what actions another user of the code can perform. Field encapsulation
involves providing methods that can be used to read or write to or from the
field rather than accessing the field directly.
The variables of the EncapTest class can be accessed using the following
program
When we look at the above example, still anyone can set age to a negative
value. But now we can control the access to that field introducing a logic.
86
Session 6 : Encapsulation
Compare the following Java classes in which the name field has not been
encapsulated:
Example 1:
Example 2:
Discussion:
In Example 1, a user is free to use the public name variable however they
see fit. In the Example 2, however the writer of the class retains control over
how the private name variable is read and written by only permitting access
to the field via its public getName and setName methods.
When you are studying level 4 course in object oriented programming you
will learn on behaviour/method encapsulation.
87
Session 6 : Encapsulation
Summary
Encapsulation is one of the fundamental object oriented programming
concept. It is used to bundle data and methods and to control the
accessibility of these bundles. Since this concept is used to hide the internal
representation from outside, encapsulation is often referred to as information
hiding. Getter and setter methods are used to implement encapsulation in
Java to get attributes and set attributes. Fields are defined as private and the
allow accessibility to these fields by defining them public.
Learning Outcomes
Now you will be able to:
Review Questions
1. State the benefits of Information hiding /encapsulation.
2. Consider the example of driving a car. Explain how the designers of
the car have used the concept of encapsulation to benefit you (the
end user).
88