0% found this document useful (0 votes)
24 views12 pages

Encapsulation

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
0% found this document useful (0 votes)
24 views12 pages

Encapsulation

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/ 12

Encapsulation

Encapsulation

What is Encapsulation?
o Encapsulation is one of the cornerstone concepts of OOP.
o Encapsulation is used to wrap up both data and methods into one
single unit.
o The way that data and methods are organized does not matter to the
end – user.
o The user is only concerned about the right way to provide input and
expects a correct output on the basis of the input provided.
Encapsulation

Importance of Encapsulation:
o To hide the internal implementation details of the class.

o Can safely modify the implementation without worrying breaking the


existing code that uses the class.

o Protect class against accidental/ willful stupidity.

o Easier to use and understand.


Encapsulation

Encapsulation using public members:


o As the name suggests, the public modifier allows variables and
functions to be accessible from anywhere within the class and from
any part of the program.

o All member variables have the access modifier as public by default.


Encapsulation

Example: Output:
Encapsulation

Encapsulation using protected members:


o What sets protected members apart from private members is that they allow
the members to be accessed within the class and allow them to be accessed
by the sub – classes involved.

o In python, we demonstrate a protected member by prefixing with an


underscore (‘_’) before its name.

o If the members have a protected access specifier it can also be referenced


then within the class and the subsequent sub-clas.
Encapsulation

Example: Output:
Encapsulation

Encapsulation using private members:


o The private access modifier allows member methods and variables to
be accessed only within the class.

o To specify a private access modifier for a member, we make use of the


double underscore(‘__’).
Encapsulation

Example: Output:
Encapsulation

Example: Output:
Encapsulation

Advantages of Encapsulation:
o Code Reusability
o Data hiding
o Improved Maintainability
o Easier to understand
o Better control over class properties
o Better class design
Thank You

You might also like