Top Banner
Encapsulat ion Githushan Gengaparam Madhusoothanan Lakshitha Imbulpitiya Sudheera Karunaratne
14

Encapsulation

Nov 19, 2014

Download

Education

Githu Shan

A short presentation about Encapsulation
OCJP Batch 27
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Encapsulation

Enca

psul

atio

n

Githushan GengaparamMadhusoothanan

Lakshitha ImbulpitiyaSudheera Karunaratne

Page 2: Encapsulation

INTRODUCTION

Encapsulation

Abstraction

Polymorphism

Inheritance

OBJECT ORIENTED PROGRAMING (OOP) CONCEPTS

Page 3: Encapsulation

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

Page 4: Encapsulation

IMPORTANCE OF ENCAPSULATION 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

Page 5: Encapsulation

WHY NEED ENCAPSULATION? 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.

Page 6: Encapsulation

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

Page 7: Encapsulation

BENEFITS OF HAVING ENCAPSULATION

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

Page 8: Encapsulation

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.

Page 9: Encapsulation

FLEXIBILITY Encapsulation makes the code easier to

visualize. This means codes can be arranged

“visually” before implementing.

Page 10: Encapsulation

EXTENSIBILITY Makes long term development easy. Updates can be made by changing the

encapsulated part without changing the input and output formats.

Page 11: Encapsulation

LIBRARY SYSTEM

+ setBaseFine(float) : void+ toString() : String+ abstract getFine(int) : float

Item <<abstract>># code : String # title : String # year : int # baseFine : float

+ toString() : String+ getFine(int) : float

Book <<entity>>

- author : String- publisher : String

+ toString() : String+ getFine(int) : float

Video <<entity>>

- producer: String- distributer: String- cast: String- duration: float

+ toString() : String+ getFine(int) : float

Audio <<entity>>

- artist: String- genre : String

Page 12: Encapsulation

CONCLUSION

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.

Page 13: Encapsulation

CONCLUSION Encapsulation makes the programing

code;

Maintainable

Extensible

Flexible

Page 14: Encapsulation