-
Notifications
You must be signed in to change notification settings - Fork 0
/
inheritance.py
31 lines (23 loc) · 1022 Bytes
/
inheritance.py
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
# Parent Class
class Person:
def __init__(self, f_name, l_name):
self.first_name = f_name
self.last_name = l_name
def print_name(self):
print(self.first_name, self.last_name) # Returns Jane Doe
# Use the Person class to create an object, and then execute the print_name method:
x = Person("Jane", "Doe")
x.print_name()
# Child Class
class Student(Person):
def __init__(self, fi_name, la_name, year):
# By using the super() function, you do not have to use the name of the parent element, it will automatically
# inherit the methods and properties from its parent.
super().__init__(fi_name, la_name)
self.graduation_year = year
def welcome(self):
print("Welcome", self.first_name, self.last_name, "to the class of", self.graduation_year, ".")
# Returns Welcome Kurt Weller to the class of 2021 .
# Use the Student class to create an object, and then execute the welcome method:
y = Student("Kurt", "Weller", 2021)
y.welcome()