How to Add a Method to Child Class in Python


In this example we will show how to add a method to the child class in Python.

Source Code

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def print_person_info(self):
        print('name:', self.name, 'age:', self.age)


class Student(Person):
    def __init__(self, name, age, grade):
        super().__init__(name, age)
        self.grade = grade

    def print_student_info(self):
        print('name:', self.name, 'age:', self.age, 'grade:', self.grade)


s = Student('John', 15, 'A')
s.print_student_info()

Output:

name: John age: 15 grade: A
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments