Python Practice › OOP Programs › Program 28

Student inherits from Person

EasyOOP Programs · 28 of 48

Create a Student class that inherits from Person, adds student_id and grades, and prints name, age and id.

Advertisement
Output
Ravi, 17, ID S-104

Approach

  1. Call super().__init__ for the shared attributes.

Solution

Python
class Person:    def __init__(self, name, age):        self.name, self.age = name, age class Student(Person):    def __init__(self, name, age, student_id, grades):        super().__init__(name, age)        self.student_id = student_id        self.grades = grades     def show(self):        print(f"{self.name}, {self.age}, ID {self.student_id}") Student("Ravi", 17, "S-104", [88, 92]).show()
Output
Ravi, 17, ID S-104

Line by line

Line 7

The parent sets name and age, so that code is not repeated.

Learn the concept

New to this topic? Read the lesson, then come back.

More oop programs

← All 48 OOP Programs

Leave a Comment