0% found this document useful (0 votes)
2 views5 pages

Encapsulation

The document explains encapsulation in programming, which secures data members and methods within a class by restricting access. It details three types of access specifiers: public, protected, and private, along with their syntax and usage. Additionally, it describes methods for accessing private members, including syntax methods, getter and setter functions, and property decorators.

Uploaded by

navikiran7975
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

Encapsulation

The document explains encapsulation in programming, which secures data members and methods within a class by restricting access. It details three types of access specifiers: public, protected, and private, along with their syntax and usage. Additionally, it describes methods for accessing private members, including syntax methods, getter and setter functions, and property decorators.

Uploaded by

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

Encapsulation

13 August 2025 07:36

Encapsulation:
-------------------------

It is phenomenon of providing security to data members or the variables and the methods
stored inside the class by restricting the access to objects.

Access Specifiers
--------------------------
These are the members of the class which will tell whether user can access them outside
the class or not.

3 types:
--------------
1) Public access specifier
2) Protected access specifier
3) Private access specifier

• Public access specifier


------------------------------

It is member of the class which will allow the user to access and modify them outside
the class.

Syntax:
class Cname:
Var = value
def mname(self,args):
--------- sb
@classmethod
def mname1(cls,args):
---------sb
@staticmethod
def method(args):
---------sb

Example :
class Z :
def __init__(self,a):
New Section 2 Page 1
def __init__(self,a):
self.a = a
def add(self,a,b):
return a+b

obj1 = Z(10)
obj2 = Z(20)

• Protected access specifiers


--------------------------------------

- This protected acts like public in python


- Which can also be access and modify outside class
- Exactly works like public access specifiers
- There is only one difference is syntax

Syntax:

class Cname:
_var = value
def _mname(self,args):
--------- sb
@classmethod
def _mname1(cls,args):
---------sb
@staticmethod
def _method(args):
---------sb

• Private access specifier


----------------------------------

- Which can be access and modify only inside the class, which will not allow the user to
access and modify outside the class.

Syntax:

class Cname:
__var = value
def __mname(self,args):
--------- sb
@classmethod
New Section 2 Page 2
@classmethod
def __mname1(cls,args):
---------sb
@staticmethod
def __method(args):
---------sb
class Company:
Cname = 'IBM'
Ceo = 'Arvind Krishna'
__no_emp = 1000
loc = 'Benguluru'

def __init__(self,ename,eid,eph,email,esal):
self.ename = ename
self.eid = eid
self.__eph = eph
self.email = email
self.__esal = esal

def disp(self):
print(self.ename,self.eid,self.email)

def __ch_sal(self,new):
self.__esal = new

@classmethod
def display(cls):
print(cls.Cname,cls.Ceo,cls.loc)

e1 = Company('Ruchitha',10113,89456230,'ruchitha@gmail',100000)
e1.disp()

print(Company.__no_emp)

3 methods
------------------
1. Syntax method
2. Using getter and setter functions
3. Property decorator

• Syntax method
---------------------

Access ---> Cname/obj._Cname__var/method(args)


New Section 2 Page 3
Access ---> Cname/obj._Cname__var/method(args)

Modify ---> Cname/obj._Cname__var/method = new_val


print(Company._Company__no_emp)

Company._Company__no_emp = 2000
print(Company._Company__no_emp)

• Using getter and setter functions


-----------------------------------------------

We will be creating the methods to get the values and set the values.

Syntax:
class Cname:
def __init__(self,var):
self.__var = var
def get_var(self):
return self.__var
def set_var(self,new):
self.__var = new

Obj = Cname(val)
class Cname:
def __init__(self,var):
self.__var = var
def get_var(self):
return self.__var
def set_var(self,new):
self.__var = new

obj = Cname(1200)
print(obj.get_var())
obj.set_var(1300)
print(obj.get_var())

• Property Decorator:
----------------------------

It is used to access any pvt member of class or object directly outside the class using a
general syntax (cls.pname,obj.pname)

class Company:
def __init__(self,sal):
self.__sal = sal

@property
def sal(self):
return self.__sal

New Section 2 Page 4


@sal.setter
def sal(self,new):
self.__sal = new

e1 = Company(25000)

print(e1.sal)

e1.sal = 30000

print(e1.sal)

New Section 2 Page 5

You might also like