Saturday, 22 April 2017

Data Abstraction

Data Abstraction:

  • The concept of hiding the properties of one class from other class  or outside of the class is known as a abstraction or data hiding.
  • Abstraction means hiding the properties of a class.
  • in order to hide the properties of a class we use _ _ beginning of the property/attribute.
  • example: _ _a = 10


class x:
    __a=100
    def display(self):
        print(x.__a)
x_obj=x( )
x_obj.display( )
print(x.__a)

100
Traceback (most recent call last):
  File "E:/python_practice/examples/data abstraction.py", line 7, in <module>
    print(x.__a)
AttributeError: type object 'x' has no attribute '__a'

Note:


  • hidden properties of a class we can access from outside of that class using special syntax.

referenceVariableName._className_ _property/attribute-name


class x:
    __a=100
    def display(self):
        print(x.__a)
x_obj=x( )
x_obj.display( )

print(x_obj._x__a)


100
100

No comments:

Post a Comment