-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInheritance.py
More file actions
186 lines (92 loc) · 3.03 KB
/
Inheritance.py
File metadata and controls
186 lines (92 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# Example 1
'''
class parent():
def skills(self):
print("I can cook and drive")
class child(parent):
def hobbies(self):
print("I love standup comedy")
obj = child()
obj.skills()
obj.hobbies()'''
# Example 2
'''
class Vehicle: # PascalCase for class names
def general_info(self):
ask = input("What you Own? (bike or car) ").lower()
if ask == "bike":
print("All bikes have 2 wheels")
return "bike"
elif ask == "car":
print("All cars have 4 wheels")
return "car"
else:
print("Invalid input")
return None
class Bike(Vehicle): # Inherits from Vehicle
def bike_info(self): # Consistent method naming
print("I've a bullet and a Splendor")
class Car(Vehicle): # Inherits from Vehicle (not from Bike)
def car_info(self): # Consistent method naming
print("I own two cars as well: 1. Astor, 2. Thar")
# Create Vehicle object
veh = Vehicle()
# Call general_info on the instance (not the class)
veh_type = veh.general_info()
if veh_type == "bike":
my_bike = Bike() # Different variable name
my_bike.bike_info()
elif veh_type == "car":
my_car = Car() # Different variable name
my_car.car_info()
'''
# 3. Practise question
class Employee:
def __init__(self , name , salary):
self.name = name
self.salary = salary
pass
def show(self):
print(f"Name = {self.name}, Salary ={self.salary}")
class Manager(Employee):
def __init__(self, name, salary,department):
super().__init__(name, salary) # Calling parent constructor
self.department = department
def show_manager(self):
print(f"Manager of {self.department} Department")
m1 = Manager("RITU RAJ","$1000","BCA")
m1.show()
m1.show_manager()
# best and good example of overriding
class SmartphoneCamera:
def take_photo(self):
print("Taking a standard Photo")
def record_video(self):
print("Recording 1080p video")
class Budgetphone(SmartphoneCamera):
pass
class FlagshipPhone(SmartphoneCamera):
def take_photo(self):
print("Taking 48MP photo with night mode ")
def record_video(self):
print("Recoding 8K videos with cinematic stabilization")
class SelfiePhone(SmartphoneCamera):
def take_photo(self):
print("Taking 32MP selfie with beauty filter")
def take_portrait(self):
print("Taking Portraits with bokeh effect")
# Create objects
budget = Budgetphone()
flagship = FlagshipPhone()
selfie = SelfiePhone()
# Test functionality
print("Budget Phone:")
budget.take_photo() # Uses parent's version
budget.record_video() # Uses parent's version
print("\nFlagship Phone:")
flagship.take_photo() # Uses overridden version
flagship.record_video() # Uses overridden version
print("\nSelfie Phone:")
selfie.take_photo() # Uses overridden version
selfie.take_portrait() # Uses new method
selfie.record_video() # Inherited from parent