Created
May 16, 2021 14:13
-
-
Save Abhayparashar31/89f1beaccc8e01f50eb2bd79461106cf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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