Data encapsulation

3.Data encapsulation:
We identified objects we needed-Time,Point and Rectangle- and defined classes to represent them.In each case there is an obvious correspondence between the object and some entity in the real world.But sometimes it is less obvious what object you need and how they should interact.In that case you need a different development plan.In the same way that we discovered function interfaces by encapsulation and generalization,we can discover class interfaces by data encapsulation.

1.Example for data encapsulation:
#!/usr/bin/env python

class Car:

    def __init__(self):
        self.__updateSoftware()

    def drive(self):
        print('driving')

    def __updateSoftware(self):
        print('updating software')

redcar = Car()
redcar.drive()
#redcar.__updateSoftware()  not accesible from object.
 
OUTPUT:
updating software
driving
Translate »