Accessible from outside the class are public members, which are typically methods declared in the class. An object belonging to the same class must be used to call a public method. The concept of data encapsulation is guaranteed by the combination of private instance variables and public methods.
A Python class’s public members are all by default. From outside the class environment, anyone can access any member.
Example:
# program to illustrate public access modifier in a class
class Student:
# constructor
def __init__(self, name, roll):
# public data members
self.Name = name
self.Roll = roll
# public member function
def display(self):
# accessing public data member
print("Roll: ", self.Roll)
# creating object of the class
obj = Student("Mayank Sharma", 31)
# accessing public data member
print("Name: ", obj.Name)
# calling public member function of the class
obj.display()
Output:
Name: Mayank Sharma
Roll: 31
A class’s protected members can be accessed from within the class and by its subclasses as well. Access to any other environment is not allowed. This makes it possible for the child class to inherit particular parent class resources.
To make an instance variable protected in Python, prefix it with (a single underscore). This effectively prevents access to it unless it comes from a subclass.
Example:
class Student:
# protected data members
_name = None
_roll = None
# constructor
def __init__(self, name, roll):
self._name = name
self._roll = roll
# protected member function
def _displayRoll(self):
# accessing protected data members
print("Roll: ", self._roll)
# derived class
class child(Student):
# constructor
def __init__(self, name, roll):
Student.__init__(self, name, roll)
# public member function
def displayDetails(self):
# accessing protected data members of super class
print("Name: ", self._name)
# accessing protected member functions of super class
self._displayRoll()
# creating objects of the derived class
obj = child("Mayank Sharma", 1706256)
# calling public member functions of the class
obj.displayDetails()
Output:
Name: Mayank Sharma
Roll: 1706256
The most secure access modifier is private, which restricts access to members of a class that have been declared private to other members of the same class only. By placing a double underscore ‘__’ symbol before a class’s data member, the data member can be made private.It gives a strong suggestion not to touch it from outside the class. Any attempt to do so will result in an Attribute Error.
Example:
class Student:
# private members
__name = None
__roll = None
# constructor
def __init__(self, name, roll):
self.__name = name
self.__roll = roll
# private member function
def __display(self):
# accessing private data members
print("Name: ", self.__name)
print("Roll: ", self.__roll)
# public member function
def accessPrivateFunction(self):
# accessing private member function
self.__display()
# creating object
obj = Student("Mayank Sharma", 1706256)
# calling public member function of the class
obj.accessPrivateFunction()
Output:
Name: Mayank Sharma
Roll: 1706256
Note: also read about Accessing Elements of 2-D Array
Please follow me to read my latest post on programming and technology if you like my post.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…