oops in python
#encapsulation
#protected access modifier
class parent:
# constructor
def __init__(self):
self._a=9
class child(parent):
def __init__(self):
#calling constructor in above
parent.__init__(self)
print(f'calling 1st one:{self._a}')
self._a=7
print(f'calling 2nd one:{self._a}')
self._a=5
print(f'calling 3rd one:{self._a}')
child()
# if i use print method it will show object also for example
print()
print(child())
print()
#creating object and accessing it
ob=parent()
ob1=child()
print(f'accseing parent class:{ob._a}')
print(f'accseing child class:{ob1._a}')#it always takes updated value
#o/p is:
calling 1st one:9
calling 2nd one:7
calling 3rd one:5
calling 1st one:9
calling 2nd one:7
calling 3rd one:5
<__main__.child object at 0x00000267DAAB62A0>
calling 1st one:9
calling 2nd one:7
calling 3rd one:5
accseing parent class:9
accseing child class:5
#public access modifier
class fa:
def __init__(self,name,age):
self.na=name
self.ae=age
def age(self):
#accesing data
print(f'age is {self.ae}')
obj_ct=fa('faizan',18)
print(f'list of methods and data:{dir(obj_ct)}')
print('name:',obj_ct.na)
obj_ct.age()
#o/p is:
list of methods and data:['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'ae', 'age', 'na']
name: faizan
age is 18
#protected access modifier
class fa:
#protected data members
_name=None
_clg=None
_branch=None
#constuctor
def __init__(self,name,clg,branch):
self._name=name
self._clg=clg
self._branch=branch
#protected data
def _data(self) :
print(f'name:{self._name}')
print(f'college:{self._clg}')
print(f'branch:{self._branch}')
#derived class
class af(fa):
#constructor
def __init__(self, name, clg, branch):
fa.__init__(self,name, clg, branch)
#or super().__init__(name,clg,branch)
#printing function
def data2(self):
self._data()
obj_1=fa('faizan','mbu','cse')
print(f'dir:{dir(obj_1)}')
obj_1._data()
print('')
obj_2=af('umar','vit','cse')
print(f'dir:{dir(obj_2)}')
obj_2.data2()
#o/p is:dir:['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_branch', '_clg', '_data', '_name']
name:faizan
college:mbu
branch:cse
dir:['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_branch', '_clg', '_data', '_name', 'data2']
name:umar
college:vit
branch:cse
# public, protected,private in acccess modifier:
#creatung a super class
class parent():
var=None #public
_var1=None #protected
__var1=None #private
# constructor
def __init__(self,var1,var2,var3):
self.var=var1
self._var1=var2
self.__var1=var3
#creating public function which can be acceseed anywhere
def public(self):
print(f'name:{self.var}')
#creating protected function which can be acceseed in subclass
def _protect(self):
print(f'protected age is{self._var1}')
#creating private function which can be acceseed only in one
def __priv(self):
print(f'private is:{self.__var1}')
#accesing private function
def dispriv(self):
self.__priv()
class child(parent):
# constructor
def __init__(self, var1, var2, var3):
super().__init__(var1, var2, var3)
#accseing protected function
def porc(self):
self._protect()
#creting and calling object
ob=child('faizan',18,'mbu')
ob.public()
ob._protect()
ob.dispriv()
print()
# pivate function can also be called
#ob._parent__var1
print(f'private data{ob._parent__var1}')
print(f'protected data:{ob._var1}')
#o/p is:
name:faizan
protected age is18
private is:mbu
private datambu
protected data:18
PS C:\third.py>
Comments
Post a Comment