Monday, 17 April 2017

Encapsulation

Encapsulation:-

  • Concept of binding or grouping related data members along with its related functionalities is known as Encapsulation

Example:-

For Customer

cname,caadr,caccno and cbal are variables
deposit( ),withdraw( ),transfer( ) and balenq( ) are methods

For Employee

eid,ename,eaddr,esal and email are variables
da( ),ta( ),hra( ),pf( ),tax( ) and tsal( ) are methods

  • class is a syntax of structure is used to grow related data members along with its related functionalities.
  • Implement the Encapsulation practically by using class syntax.


syntax:

class  <class_name>:
             """ doc string """
            ..................
            ..................

            ..................

print("start")
class Test:
""" this is sample class"""
x=10
def m1(self):
print("m1 in Test")
x=Test.x+500
print("end")
t1=Test()
t1.m1()
t1.m1()
t2=Test()
t2.m1()
t2.m1()

start
end
m1 in Test
m1 in Test
m1 in Test
m1 in Test


class Test:
def m1(self):
self.x=10
t1=Test()
t1.m1()
print(t1.x)
t1.x=t1.x+5
print(t1.x)
t2=Test()
t2.m1()
print(t2.x)
t2.x=t2.x+2
print(t2.x)
t3=Test()
t3.m1()
print(t3.x)

10
15
10
12
10

Constructor:-
  • Constructor is a special kind of method , which is used to initialize the non-static variables of a class at the time of creation of the Object.
  • Constructor will be executed automatically.
Syntax:

def  __init__(self, ...):
             -------------------
             -------------------
             -------------------


class X:

    def  __init__(self): -----> constructor
   print("Constructor of X")
    def m1(self):   ---------> method
   print("In m1 of X")
x1=X( )
x1.m1( )


Constructor of X
In m1 of X


class X:
    def __init__(self):
   self.i=10
   self.j=20
    def disp(self):
   print(self.i)
   print(self.j)
x1=X( )
x1.disp( )
x1.i=30
x1.j=40
x1.disp( )
x2=X( )
x2.disp( )
x2.i=50
x2.j=60
x2.disp( )

10
20
30
40
10
20
50
60

class X:
    def __init__(self,i,j):
   self.i=i;
   self.j=j
    def disp(self):
   print(self.i)
   print(self.j)
x1=X(10,20)
x1.disp( )
x2=X(30,40)
x2.disp( )

10
20
30
40

Differences between the method and the Constructor


                 Methods  
            Constructors

Method name can be Anything or any name     
Constructor name should be  _ _init_ _

After creating the object if we call a method then only method will be executed.
Constructor will be executed automatically whenever we created object

with respect to  one object one method can be called for any number of times
with respect to one object one constructor will be executed only once
Methods are used to represent the business logics to perform the operations
Constructors are used to initialize the non-static variables.

why method takes self parameter:

  • when objects are instantiated,the object itself is passed in to the self parameter of the method.


Garbage Collector:-


  • It is a pre-defined program which is present inside python software.
  • Garbage collector removes the unused or unreferenced objects from the memory location. If the reference count of any object is 0(zero) then we call that object as a unreferenced or unused object. 
  • Whenever any object reference count is zero then python interpreter calls the Garbage collector and GC(Garbage collector) removes the object. 
  • After executing the GC more memory is available for the program so that rest of the program execution becomes faster.



class X:

    def  __init__(self):

   print("in constructor of X")

    def  __del__(self):

   print("in destructor of X")

x1=X( )

x2=x1

x2=X( )

x1=X( )


in constructor of X

in constructor of X

in constructor of X

in destructor of X


  • Before going to delete the objects from the memory location it internally executes destructor.
  • _ _del_ _ is known as a destructor.
  • del is a keyword, which is used for removing the attributes.


class X:


    def  __init__(self):

   print("in constructor of X")

    def  __del__(self):

   print("in destructor of X")
x1=X()

x2=X()

x3=X()

del x1

del x2

del x3


in constructor of X

in constructor of X

in constructor of X

in destructor of X

in destructor of X

in destructor of X

No comments:

Post a Comment