Python InheritanceInheritance allows us to define a class that inherits all the methods and properties from another class. Show
Parent class is the class being inherited from, also called base class. Child class is the class that inherits from another class, also called derived class. Create a Parent ClassAny class can be a parent class, so the syntax is the same as creating any other class: ExampleCreate a class named class Person: def printname(self): #Use the Person class to create an object, and then execute the printname method: x = Person("John", "Doe") Try it Yourself » Create a Child ClassTo create a class that inherits the functionality from another class, send the parent class as a parameter when creating the child class: ExampleCreate a class named class Student(Person): Note:
Use the Now the Student class has the same properties and methods as the Person class. ExampleUse the x = Student("Mike", "Olsen") Try it Yourself » Add the __init__() FunctionSo far we have created a child class that inherits the properties and methods from its parent. We want to add the Note: The ExampleAdd the class Student(Person): When you add the Note: The child's To keep the inheritance of the parent's Example class Student(Person): Try it Yourself » Now we have successfully added the __init__() function, and kept the inheritance of the parent class, and we are ready to add functionality in the Use the super() FunctionPython also has a Example class Student(Person): Try it Yourself » By using the Add PropertiesExampleAdd a property called class Student(Person): Try it Yourself » In the example below, the year ExampleAdd a class Student(Person): x = Student("Mike", "Olsen", 2019) Try it Yourself » Add MethodsExampleAdd a method called class Student(Person): def welcome(self): Try it Yourself » If you add a method in the child class with the same name as a function in the parent class, the inheritance of the parent method will be overridden. Test Yourself With ExercisesExercise:What is the correct syntax to create a class named Student that will inherit properties and methods from a class named Person? Start the Exercise What keyword is used to call the methods or fields of the parent class?We can use super keyword to access the data member or field of parent class. It is used if parent class and child class have same fields.
What is inherited method?The inherited methods can be used directly as they are. You can write a new instance method in the subclass that has the same signature as the one in the superclass, thus overriding it. You can write a new static method in the subclass that has the same signature as the one in the superclass, thus hiding it.
What methods can be inherited by the child class?Child classes inherit accessible fields and methods from their parent classes and other ancestors. They never inherit constructors, however. Instead, child classes declare their own constructors.
What keyword allows a child class to access its parent class's implementation of a method that the child class is overriding?If there is a non-private method defined in the parent class you can override this method in the child class by using the same return type and parameters (signature). You can access the method of the parent class by using the super. keyword.
|