Showing posts with the label Python - Encapsulation

Python Object Oriented Programming - Encapsulation

Encapsulation is the process of combining variables and methods into one object. It's one of the fundamental ideas in Object-Oriented Programming (OOP). Encapsulation acts as a shield that prevents direct access to variables and methods, and prevents accidental or unauthorized changes to data. It also turns objects into more autonomous, self-managing entities.  Encapsulation is achieved through the use of access modifiers such as 'public', 'private', and 'protected'. Access modifiers Access modifiers restrict access to  variables and functions in a class.  Python uses three types of access modifiers.  They are  private, public, and protected. Public members Public members can be accessed from anywhere within the class. All member variables of a class are public by default. # program to illustrate public access modifier in a class   class User:             # constructor       def __init__(self, name, age):                         # public data members