computer scince and engineeringpython

python Classes and Objects Encapsulation in hindi

3views

•Classes and Objects

•Encapsulation

Class

•The class can be defined as a collection of objects.

•It is a logical entity that has some specific attributes and methods.

•For example: if you have an employee class then it should contain an attribute and method, i.e. an email id, name, age, salary, etc.

Example

class ClassName:   

        <statement-1>   

        .   

        .    

        <statement-N> 

Example of Class

class Test:

    # A sample method 

    def fun(self):

        print(“Hello”)

# Driver code

obj = Test()

obj.fun()

Object

•The object is an entity that has state and behavior.

•It may be any real-world object like the mouse, keyboard, chair, table, pen, etc.

•Everything in Python is an object, and almost everything has attributes and methods.

•All functions have a built-in attribute __doc__, which returns the doc string defined in the function source code.

Method

•The method is a function that is associated with an object.

•In Python, a method is not unique to class instances.

•Any object type can have methods.

Encapsulation

  • Encapsulation is also an essential aspect of object-oriented programming. 
  • It is used to restrict access to methods and variables. 
  • In encapsulation, code and data are wrapped together within a single unit from being modified by accident.

Data Abstraction

•Data abstraction and encapsulation both are often used as synonyms.

•Both are nearly synonym because data abstraction is achieved through encapsulation.

•Abstraction is used to hide internal details and show only functionalities.

•Abstracting something means to give names to things so that the name captures the core of what a function or a whole program does.

Leave a Response