Python Create Child Class
Create a Child Class
To create a class that inherits the functionality from another class, send the parent class as a parameter when creating the child class:
Example
Create a class named Student
, which will inherit the properties
and methods from
the Person
class:
class Student(Person):
pass
Note: Use the pass
keyword when you do not want to add any other properties or methods to the
class.
Now the Student class has the same properties and methods as the Person class.
Example
Use the Student
class to create an object,
and then execute the printname
method:
x = Student("Mike", "Olsen")
x.printname()
Try it Yourself »
Copyright 1999-2023 by Refsnes Data. All Rights Reserved.