100% found this document useful (1 vote)
807 views10 pages

Encapsulation Presentation

Encapsulation is a process of combining data and functions into a single unit called a class. It involves making the fields in a class private and providing public methods to access and modify the fields. This prevents unauthorized access and manipulation of data, and allows one class to interact with another through a controlled interface. Encapsulation improves flexibility, reusability, and maintainability of code by hiding implementation details and allowing changes without affecting other code.

Uploaded by

priya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
807 views10 pages

Encapsulation Presentation

Encapsulation is a process of combining data and functions into a single unit called a class. It involves making the fields in a class private and providing public methods to access and modify the fields. This prevents unauthorized access and manipulation of data, and allows one class to interact with another through a controlled interface. Encapsulation improves flexibility, reusability, and maintainability of code by hiding implementation details and allowing changes without affecting other code.

Uploaded by

priya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

TOPIC:

ENCAPSULATION
WHAT IS ENCAPSULATION?

● Encapsulation is a process
of combining data and
functions into a single unit
called class.
● Encapsulation is technique
of making fields in a class
private and providing
access to the fields via
public methods.
EXAMPLE CODE
#include<iostream.h>
using namespace std;

class Encapsulation
{
private:
int x;
public:
void set(int a)
{
x=a;
}
void get()
{
return x;
}
};

int main()
{
Encapsulation obj;
cout<<obj.x; //Error!
obj.set(5); //Correct way!
cout<<obj.get();
return 0;
}
Why Is Encapsulation Important?
● Flexibility: It’s more flexible and easy to change the encapsulated
code with new requirements. For example, if the requirement for
setting the age of a person changes, we can easily update the logic in
the setter method.
● Reusability: Encapsulated code can be reused throughout multiple
applications. For example, the Person class can be reused whenever
such type of object is required.
● Maintainability: Code is encapsulated in separate units (classes,
interfaces, methods, setters, getters, etc) so it’s easy to change or
update a part of the application without affecting other parts, which
reduces the time of maintenance.
Difference between Abstraction and
Encapsulation
● Encapsulation means hiding
● Abstraction lets you focus on
the internal details or
what the object does instead of
mechanism of how an object
how it does.
does something.
● It solves the problem in Design
● It solves the problem in
level.
Implementation level.
● Ex: Outer look of a mobile phone,
● Ex: Inner implementation
like it has a display screen and
detail of a mobile phone, how
keypad buttons to dial a number.
keypad button and display
screen are connect with each
other using circuits.
Advantages of Encapsulation
● To allow one class(“server”) to make a interact with
another class(“client”).
● Data and function may be public or private.
● It hides the complexity from the implementation.
● It also provides inter independencies.
● A class can have total control over what is stored in
its fields.
Use Case:
Conclusion:
● It is one of the important
feature of OOPS.
● It is used to wrap the data
and function together in a
single unit.
● Thus, it also provide inter
independencies.
THANK YOU!

You might also like