You are on page 1of 10

Chapter 2

Encapsulation

Dyah Fajar Nur Rohmah

Introduction
Encaptulation is the technique of making the fields
in a class private and providing access to the fields
via public methods.

Encapsulation also can be described as a protective


barrier that prevents the code and data being

randomly accessed by other code defined outside


the class.

Why need encapsulation?


Combining data and how it's manipulated in one
place: This is achieved through the state (the
private fields) and the behaviors (the public
methods) of an object.

Only allowing the state of an object to be accessed


and modified through behaviors: The values

contained within an object's state can then be


strictly controlled.

Why need encapsulation? (cont.. )


Hiding the details of how the object works: The
only part of the object that is accessible to the

outside world is its behaviors. What happens inside


those behaviors and how the state is stored is
hidden from view.

public functions

private
functions

private data

Maintainability
Encapsulation draws a boundary around a set of
data and method.

This allows a developer to use the code without

having to know how it works, but with what input


data and its input data range and with what it
returns.

Flexibility
Encapsulation makes the code easier to visualize.

This means codes can be arranged visually before


implementing.

Extensibility
Makes long term development easy.

Updates can be made by changing the

encapsulated part without changing the input and


output formats.

Kesimpulan
Encapsulation is the technique of making the fields

in a class private and providing access to the fields


via public methods.

The main benefit of encapsulation is the ability to


modify the implemented code without breaking

the code of others who use the implemented code.

Kesimpulan (cont.. .)
Encapsulation makes the programing code:

Flexi
ble

Main
taina
ble

Exte
nsibl
e

Thank You

Reference from:
Githushan Gengaparam - Madhusoothanan
Lakshitha Imbulpitiya - Sudheera Karunaratne

You might also like