Object-oriented programming · 17 / 26 Exercise Medium

Inheritance of objects

Objective

Develop a Python program that asks the user for three names and stores them in a list of Person objects. Two of them are students and one is a teacher.

Instructions

  • Create a class called Person with an attribute name, a constructor that receives the name and the special method __str__.
  • Create two classes that inherit from Person: Student and Teacher. Each one has a constructor that calls the constructor of the parent class with super().__init__().
  • The Student class has the method study(), which displays that the student is studying. The Teacher class has the method explain(), which displays that the teacher is explaining.
  • Ask the user for the name of the teacher and the names of the two students.
  • Go through the list and call explain() or study() according to the type of each object. Notice how inheritance and polymorphism make the code more organized.
Input
Juan
Sara
Carlos
Output
Explain
Study
Study

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise