© Prognoz Technologies Pvt. Ltd
Encapsulation
© Prognoz Technologies Pvt. Ltd
OOP
Encapsulation
Inheritance
Polymorphism
Abstraction
4 pillars of OOPS
© Prognoz Technologies Pvt. Ltd
Encapsulation
 Hiding implementation details of an object from its clients.
 Encapsulation provides abstraction.
 separates external view (behavior) from internal view (state)
 Encapsulation protects the integrity of an object's data.
© Prognoz Technologies Pvt. Ltd
Defining Encapsulation
4
 Encapsulation is the process of hiding an object’s implementation from another object, while
presenting only the interfaces that should be visible.
 An encapsulated object can be thought of as a black box or an abstraction.
 Its inner workings are hidden to the client, which only invokes the interface methods.
A=10
B=10
C=20
Addition
© Prognoz Technologies Pvt. Ltd
1st Principles of Encapsulation
“Don’t ask how I do it, but this is what I can do”
- The encapsulated object
5
How this thing
sending message?
© Prognoz Technologies Pvt. Ltd
2nd Principles of Encapsulation
“I don’t care how, just do your job, and I’ll do mine”
- One encapsulated object to another
6
© Prognoz Technologies Pvt. Ltd
Encapsulating a Class
 Members of a class must always be declared with the minimum level of visibility.
 Provide setters and getters (also known as accessors/mutators) to allow controlled access to
private data.
 Provide other public methods (known as interfaces ) that other objects must adhere to in order
to interact with the object.
7
Setters and Getters
private char sex;
public void setSex(char s) {
// validate here
sex = s;
}
public char getSex() {
// format here
return sex;
}
 Setters are methods that (only) alter the state of an object
 Getters are methods that (only) return information about the state of an object
© Prognoz Technologies Pvt. Ltd
Example
9
© Prognoz Technologies Pvt. Ltd
Benefits of encapsulation
 Provides abstraction between an object and its clients.
 Protects an object from unwanted access by clients.
Ex: A bank app forbids a client to change an Account's balance.
© Prognoz Technologies Pvt. Ltd
Thank you!!
11

Concept of Object Oriented Programming

  • 1.
    © Prognoz TechnologiesPvt. Ltd Encapsulation
  • 2.
    © Prognoz TechnologiesPvt. Ltd OOP Encapsulation Inheritance Polymorphism Abstraction 4 pillars of OOPS
  • 3.
    © Prognoz TechnologiesPvt. Ltd Encapsulation  Hiding implementation details of an object from its clients.  Encapsulation provides abstraction.  separates external view (behavior) from internal view (state)  Encapsulation protects the integrity of an object's data.
  • 4.
    © Prognoz TechnologiesPvt. Ltd Defining Encapsulation 4  Encapsulation is the process of hiding an object’s implementation from another object, while presenting only the interfaces that should be visible.  An encapsulated object can be thought of as a black box or an abstraction.  Its inner workings are hidden to the client, which only invokes the interface methods. A=10 B=10 C=20 Addition
  • 5.
    © Prognoz TechnologiesPvt. Ltd 1st Principles of Encapsulation “Don’t ask how I do it, but this is what I can do” - The encapsulated object 5 How this thing sending message?
  • 6.
    © Prognoz TechnologiesPvt. Ltd 2nd Principles of Encapsulation “I don’t care how, just do your job, and I’ll do mine” - One encapsulated object to another 6
  • 7.
    © Prognoz TechnologiesPvt. Ltd Encapsulating a Class  Members of a class must always be declared with the minimum level of visibility.  Provide setters and getters (also known as accessors/mutators) to allow controlled access to private data.  Provide other public methods (known as interfaces ) that other objects must adhere to in order to interact with the object. 7
  • 8.
    Setters and Getters privatechar sex; public void setSex(char s) { // validate here sex = s; } public char getSex() { // format here return sex; }  Setters are methods that (only) alter the state of an object  Getters are methods that (only) return information about the state of an object
  • 9.
    © Prognoz TechnologiesPvt. Ltd Example 9
  • 10.
    © Prognoz TechnologiesPvt. Ltd Benefits of encapsulation  Provides abstraction between an object and its clients.  Protects an object from unwanted access by clients. Ex: A bank app forbids a client to change an Account's balance.
  • 11.
    © Prognoz TechnologiesPvt. Ltd Thank you!! 11