You are on page 1of 14

Encapsulation

Githushan Gengaparam
Madhusoothanan
Lakshitha Imbulpitiya
Sudheera Karunaratne
Encapsulation
Inheritance

Polymorphism Abstraction
 Encapsulation 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.
 To hide the internal implementation details of the
class

 Can safely modified the implementation without


worrying breaking the existing code that uses the
class

 Protect class against accidental/ willful stupidity

 Keeps class tidy by keeping the visible fields to a


minimum

 Easier to use and understand


 In object-oriented programming data encapsulation is
concerned with;
1. 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.

2. 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.

3. 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.
 The main benefit of encapsulation is the
ability to modify our implemented code
without breaking the code of others who use
our code.
 With this feature Encapsulation gives
maintainability, flexibility and extensibility
to the code.
 The fields of a class can be made read-only or
write- only

 A class can have total control over what is stored


in its fields

 The users of a class do not know how the class


stores its data.

 A class can change the data type of a fields, and


a users of the class do not need to change any of
their code
 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.
 Encapsulation makes the code easier to
visualize.
 This means codes can be arranged “visually”
before implementing.
 Makes long term development easy.
 Updates can be made by changing the
encapsulated part without changing the input
and output formats.
Item <<abstract>>
# code : String
# title : String
# year : int
# baseFine : float
+ setBaseFine(float) : void
+ toString() : String
+ abstract getFine(int) : float

Book <<entity>> Video <<entity>> Audio <<entity>>


- producer: String
- author : String - distributer: String - artist: String
- publisher : String - cast: String - genre : String
- duration: float

+ toString() : String + toString() : String + toString() : String


+ getFine(int) : float + getFine(int) : float + getFine(int) : float
 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.
 Encapsulation makes the programing code;

Flexible

Extensible

Maintaina
ble

You might also like