Python Inheritance
- Previous Page Python Class/Object
- Next Page Python Iteration
Python Inheritance
Inheritance allows us to define a class that inherits all methods and properties from another class.
SuperclassIs an inherited class, also known as a base class.
SubclassIs a class that inherits from another class, also known as a derived class.
Create a superclass
Any class can be a superclass, so the syntax is the same as creating any other class:
Instance
Create a class named Person that includes the firstname and lastname attributes, as well as the printname method:
class Person: def __init__(self, fname, lname): self.firstname = fname self.lastname = lname self.firstname = fname self.lastname = lname def printname(self): print(self.firstname, self.lastname) Create an object using the Student class and then execute the printname method:
# Use Person to create an object, and then execute the printname method:
x = Person("Bill", "Gates")
Instance
Create a subclass
class Student(Person): __init__()
# Add properties, etc.To create a class that inherits functionality from other classes, send the parent class as a parameter when creating the subclass: __init__()
Create a class named Student, which will inherit properties and methods from the Person class:
If you do not want to add any other properties or methods to the class, use
Instance
Keyword.
Now, the Student class has the same properties and methods as the Person class. Create an object using the Student class and then execute the printname method:
x = Student("Elon", "Musk")
x.printname()
Add __init__() function So far, we have created a subclass that inherits the properties and methods of the parent class.
We want to put __init__()
function to the subclass (not
# Add properties, etc.pass
Instance
Keyword).
class Student(Person): def __init__(self, fname, lname): The __init__() function is automatically called each time a new object is created using the class.
Add the __init__() function to the Student class:
# Add properties, etc.When you add the __init__() function, the subclass will no longer inherit the parent's __init__() function.
Comments:
Instance
class Student(Person): def __init__(self, fname, lname): The child's __init__() function will override the inheritance of the parent's __init__() function.
If you want to keep the inheritance of the parent's __init__() function, please add a call to the parent's __init__() function:
Person.__init__(self, fname, lname)
Now that we have successfully added the __init__() function and retained the inheritance of the parent class, we are ready to add features in the __init__() function. By using the function, it will make the subclass inherit all methods and properties from its parent:
using the super() function
Instance
class Student(Person): def __init__(self, fname, lname): super().__init__(fname, lname)
Python also has By using the function, it will make the subclass inherit all methods and properties from its parent:
super()
add properties function, you do not need to use the name of the parent element, it will automatically inherit methods and properties from its parent element.
Instance
Named graduationyear
add properties to Student
Class:
class Student(Person): def __init__(self, fname, lname): super().__init__(fname, lname) self.graduationyear = 2019
In this example, 2019 should be a variable, and passed to the Student class when creating the student object. To do this, add another parameter to the __init__() function:
Instance
Add year
Parameters, and pass the correct year when creating an object:
class Student(Person): def __init__(self, fname, lname, year) super().__init__(fname, lname) self.graduationyear = year x = Student("Elon", "Musk", 2019)
Add Method
Instance
Named welcome
Add the method to the Student class:
class Student(Person): def __init__(self, fname, lname, year): super().__init__(fname, lname) self.graduationyear = year def welcome(self): print("Welcome", self.firstname, self.lastname, "to the class of", self.graduationyear)
Tip:If you add a method with the same name as a function in the superclass in the subclass, the superclass method will be overridden by inheritance.
- Previous Page Python Class/Object
- Next Page Python Iteration