Encapsulation with Access Modifiers in Python

Encapsulation in Python :

Definition :

  • Encapsulation is the concept of restricting direct access to an object’s data and methods to protect data integrity.


Access Modifiers :

  • Accessible only within the class.
  • Accessible within the class and its subclasses.
  • Accessible from anywhere.

    

Encapsulation without Access Modifiers :

Program:




Output :




Encapsulation with Access Modifiers

Private :

Definition: Variables or methods prefixed with are private and not accessible directly

 outside the class.


Program:


Output :




Protected : 

Definition: 

  • Variables or methods prefixed with are protected and can be accessed within the class or its subclasses.

Program:


Output :


Public :

Definition :

  • Variables or methods without any prefix are public and accessible from anywhere.


Program:


Output :


Post a Comment

0 Comments