|
1 |
| -class Parent(): |
2 |
| - def __init__(self, last_name, eye_color): |
3 |
| - print ("Parent Constructor Called") |
4 |
| - self.last_name = last_name |
5 |
| - self.eye_color = eye_color |
6 |
| - |
7 |
| - def show_info(self): # This method is also available to class Child |
8 |
| - print (self.last_name) |
9 |
| - print (self.eye_color) |
10 |
| - |
11 |
| -class Child(Parent): |
12 |
| - |
13 |
| - def __init__(self, last_name, eye_color, number_of_toys): |
14 |
| - print ("Child Constructor Called") |
15 |
| - Parent.__init__(self, last_name, eye_color) |
16 |
| - self.number_of_toys = number_of_toys |
17 |
| - |
18 |
| - def show_info(self): # Method Overriding (ability of a subclass to override a method defined in the parent class) |
19 |
| - print (self.last_name) |
20 |
| - print (self.eye_color) |
21 |
| - print (self.number_of_toys) |
22 |
| - |
23 |
| -billy_cyrus = Parent ("Cyrus", "Blue") |
24 |
| -miley_cyrus = Child ("Cyrus", "blue", 5) |
25 |
| -#print (miley_cyrus.last_name) |
26 |
| -#print (miley_cyrus.number_of_toys) |
27 |
| - |
28 |
| - |
29 |
| -#print (billy_cyrus.eye_color) |
30 |
| - |
31 |
| -miley_cyrus.show_info() |
| 1 | +class Parent(): |
| 2 | + def __init__(self, last_name, eye_color): |
| 3 | + print ("Parent Constructor Called") |
| 4 | + self.last_name = last_name |
| 5 | + self.eye_color = eye_color |
| 6 | + |
| 7 | + def show_info(self): # This method is also available to class Child |
| 8 | + print (self.last_name) |
| 9 | + print (self.eye_color) |
| 10 | + |
| 11 | +class Child(Parent): |
| 12 | + |
| 13 | + def __init__(self, last_name, eye_color, number_of_toys): |
| 14 | + print ("Child Constructor Called") |
| 15 | + Parent.__init__(self, last_name, eye_color) |
| 16 | + self.number_of_toys = number_of_toys |
| 17 | + |
| 18 | + def show_info(self): # Method Overriding (ability of a subclass to override a method defined in the parent class) |
| 19 | + print (self.last_name) |
| 20 | + print (self.eye_color) |
| 21 | + print (self.number_of_toys) |
| 22 | + |
| 23 | +billy_cyrus = Parent ("Cyrus", "Blue") |
| 24 | +miley_cyrus = Child ("Cyrus", "blue", 5) |
| 25 | +#print (miley_cyrus.last_name) |
| 26 | +#print (miley_cyrus.number_of_toys) |
| 27 | + |
| 28 | + |
| 29 | +#print (billy_cyrus.eye_color) |
| 30 | + |
| 31 | +miley_cyrus.show_info() |
0 commit comments