Skip to content

Commit b8b904e

Browse files
data Encapsulation added
1 parent 08ff1d5 commit b8b904e

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

Advance_Inhertance.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,36 @@ class Person:
1414
def __init__(self, name, age):
1515
self.name = name
1616
self.age = age
17-
self.country = 'Pakistan'
17+
self.country = 'Pakistan' # Protected Variable.
18+
self.__private = 'secret' # Private variable
1819

1920
def __str__(self):
20-
return 'Hello to Person Class'
21+
return f'Hello to Person Class {self.name}'
2122

2223
class Employee(Person):
2324
# Need to assign Values otherwise through Error.
24-
def __init__(self):
25-
Person.name = 'Hassan'
26-
Person.age = '21'
25+
def __init__(self, name, age):
26+
Person.__init__(self, name, age)
27+
Person.name = 'Hassan' # not work
28+
Person.age = '21' # not work, logic.
29+
30+
print(self.country) # work.
31+
print(self.__private) # Through error.
32+
33+
34+
2735

2836
@staticmethod
2937
def show():
3038
print('Its Employee Class')
3139

3240
# Objects / Instance
33-
emp1 = Employee()
41+
emp1 = Employee('hassan', 22)
3442
print(emp1)
3543
emp1.show()
3644
print(emp1.name)
37-
print(emp1.country) # Through error. Not recognize.
45+
print(emp1.country) # Now, it works
46+
print(emp1.__private) # Now, it not work, through error.
3847

3948

4049
# ====== Inheritance Super Keyword ======= #
@@ -68,10 +77,10 @@ def is_employee(self):
6877
# Objects / Instance
6978
emp1 = Employee('Ali', 22, 'Real Estate Agent')
7079
print(emp1.show())
71-
print(emp1)
80+
print(emp1) # gives Hassan
7281
print(emp1.is_employee())
7382

7483

7584
if __name__ == '__main__':
76-
# inheritance_basics()
77-
super_keyword()
85+
inheritance_basics()
86+
# super_keyword()

0 commit comments

Comments
 (0)