# program to illustrate public access modifier in a class
class Geek:
# constructor
def __init__(self, name, age):
# public data members
[Link] = name
[Link] = age
# public member function
def displayAge(self):
# accessing public data member
print("Age: ", [Link])
# creating object of the class
obj = Geek("R2J", 20)
# finding all the fields and methods which are present inside obj
print("List of fields and methods inside obj:", dir(obj))
# accessing public data member
print("Name:", [Link])
# calling public member function of the class
[Link]()
# program to illustrate protected access modifier in a class
# super class
class Student:
# protected data members
_name = None
_roll = None
_branch = None
# constructor
def __init__(self, name, roll, branch):
self._name = name
self._roll = roll
self._branch = branch
# protected member function
def _displayRollAndBranch(self):
# accessing protected data members
print("Roll:", self._roll)
print("Branch:", self._branch)
# derived class
class Geek(Student):
# constructor
def __init__(self, name, roll, branch):
Student.__init__(self, name, roll, branch)
# 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._displayRollAndBranch()
stu = Student("Alpha", 1234567, "Computer Science")
print(dir(stu))
# protected members and methods can be still accessed
print(stu._name)
stu._displayRollAndBranch()
# Throws error
# print([Link])
# [Link]()
# creating objects of the derived class
obj = Geek("R2J", 1706256, "Information Technology")
print("")
print(dir(obj))
# calling public member functions of the class
[Link]()
# program to illustrate private access modifier in a class
class Geek:
# private members
__name = None
__roll = None
__branch = None
# constructor
def __init__(self, name, roll, branch):
self.__name = name
self.__roll = roll
self.__branch = branch
# private member function
def __displayDetails(self):
# accessing private data members
print("Name:", self.__name)
print("Roll:", self.__roll)
print("Branch:", self.__branch)
# public member function
def accessPrivateFunction(self):
# accessing private member function
self.__displayDetails()
# creating object
obj = Geek("R2J", 1706256, "Information Technology")
print(dir(obj))
print("")
# Throws error
# obj.__name
# obj.__roll
# obj.__branch
# obj.__displayDetails()
# To access private members of a class
print(obj._Geek__name)
print(obj._Geek__roll)
print(obj._Geek__branch)
obj._Geek__displayDetails()
print("")
# calling public member function of the class
[Link]()