Skip to content

Commit 7365bde

Browse files
authored
Update README.md
1 parent e024286 commit 7365bde

File tree

1 file changed

+6
-6
lines changed
  • exercises/043-inheritance_and_polymorphism

1 file changed

+6
-6
lines changed

exercises/043-inheritance_and_polymorphism/README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# `043` inheritance and polymorphism
1+
# `043` Inheritance and polymorphism
22

33
Now that we understand what a class is and some of its characteristics, let's talk about two new concepts related to classes: inheritance and polymorphism. Consider the following example:
44

@@ -9,12 +9,12 @@ class HighSchoolStudent(Student): # Add the parent class inside the parenthesis
99
self.specialization = specialization
1010

1111
def study(self, hours):
12-
print(f"{self.name} is a high school student specializing in {self.specialization} and is studying for {hours} hours for exams.")
12+
return f"{self.name} is a high school student specializing in {self.specialization} and is studying for {hours} hours for exams."
1313

1414
# Creating an instance of HighSchoolStudent
1515
high_school_student = HighSchoolStudent("John", 16, 85, "Science")
16-
high_school_student.introduce() # We can call this method thanks to inheritance
17-
high_school_student.study(4) # This method has been slightly modified and now it prints a different string
16+
print(high_school_student.introduce()) # We can call this method thanks to inheritance
17+
print(high_school_student.study(4)) # This method has been slightly modified and now it returns a different string
1818
```
1919

2020
Assuming that the `Student` class from the previous exercise is coded just above this `HighSchoolStudent` class, to inherit its methods and attributes, we simply include the name of the class we want to inherit from (the parent class) inside the parentheses of the child class (`HighSchoolStudent`). As you can see, we can now use the `introduce` method from the `Student` class without having to code it again, making our code more efficient. The same applies to attributes; we don't need to redefine them.
@@ -27,13 +27,13 @@ Additionally, we have the flexibility to add new methods exclusively for this cl
2727

2828
2. Add a new attribute called `major` to represent the major they are studying.
2929

30-
3. Modify the inherited `introduce` method to print this string:
30+
3. Modify the inherited `introduce` method to return this string:
3131

3232
```py
3333
"Hi there! I'm <name>, a college student majoring in <major>."
3434
```
3535

36-
4. Add a new method called `attend_lecture` that prints the following string:
36+
4. Add a new method called `attend_lecture` that returns the following string:
3737

3838
```py
3939
"<name> is attending a lecture for <major> students."

0 commit comments

Comments
 (0)