Skip to content

Instantly share code, notes, and snippets.

@Abhayparashar31
Created May 16, 2021 14:13
Show Gist options
  • Save Abhayparashar31/89f1beaccc8e01f50eb2bd79461106cf to your computer and use it in GitHub Desktop.
Save Abhayparashar31/89f1beaccc8e01f50eb2bd79461106cf to your computer and use it in GitHub Desktop.
class Parent: ## Creating a class name Parent
def __init__(self): ## Constructor of parent class
# protected member
self._mobilenumber = 5555551234 ## Protected member of the class Parent
class Child(Parent): ## Child class inhering properties from the Parent class
def __init__(self): ## Constructor of the class name
Parent.__init__(self) ## accessing members of the Parent class, another way is to used supre()
print("Calling Protected Member")
print(self._mobilenumber) ## accessing protected member using the class member
obj = Child() ## creating the object
print(obj.mobilenumber) ## AttributeError: 'Child' object has no attribute 'mobilenumber'
print(obj._mobilenumber) ## Prints mobilenumber, explicitly allowing the access to protected member
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment