Python Inheritance


Python Inheritance

Inheritance allows us to define a category that inherits all methods and structures in another category.

The parent category is the inherited category, also called the base class.

A children's class is a class that benefits from another class, also called an derived class.


Create a Parent Class

Any class can be a parent class, so syntax is the same as creating any other category:


Example

Create a class named Person, with firstname and lastname properties, and a printname method:

class Person:
  def __init__(self, fname, lname):
    self.firstname = fname
    self.lastname = lname

  def printname(self):
    print(self.firstname, self.lastname)

#Use the Person class to create an object, and then execute the printname method:

x = Person("John", "Doe")
x.printname()


Create a Child Class

To create a legacy class to work in another class, submit the parent category as a parameter when creating a 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 a pass if you do not want to add other features or methods to the class.


The student class now has the same features and methods as the Man class.


Example

Use the Student class to create an object, and then execute the printname method:

x = Student("Mike", "Olsen")
x.printname()


Add the __init__() Function

So far we have built a children's classroom that inherits buildings and structures from its parent.

We want to add __init __ () function to the children's class (instead of a pass key).


Note: The __ init __ () function is automatically called every time a class is used to create something new.


Example

Add the __init__() function to the Student class:

class Student(Person):
  def __init__(self, fname, lname):
    #add properties etc.

If you add __init __ () function, the child category will no longer inherit parent __init __ () function.


Note: A child's __init __() function exceeds the parent __init __() parent's legacy.


To save a parent __init __() parent's job asset, add a call to the parent __init __() parent:


Example
class Student(Person):
  def __init__(self, fname, lname):
    Person.__init__(self, fname, lname)

We have now successfully added the __init __ () function, and saved the parental legacy, and are ready to add functionality to __init __() function.


Use the super() Function

Python also has a super() functionality that will enable the kids category to benefit from all the features and structures from its parent:


Example
class Student(Person):
  def __init__(self, fname, lname):
    super().__init__(fname, lname)

By using the super() function, you do not need to use the parent element name, it will automatically acquire methods and structures from its parent.


Add Properties


Example

Add a property called graduationyear to the Student class:

class Student(Person):
  def __init__(self, fname, lname):
    super().__init__(fname, lname)
    self.graduationyear = 2019

In the example below, the year 2019 should be flexible, and move beyond the Student body when creating student items. To do so, add another parameter to __ init __ () function:


Example

Add a year parameter, and pass the correct year when creating objects:

class Student(Person):
  def __init__(self, fname, lname, year):
    super().__init__(fname, lname)
    self.graduationyear = year

x = Student("Mike", "Olsen", 2019)


Add Methods


Example

Add a method called welcome 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)

If you add a path to a child class with the same name as the parent category function, the parent path path inheritance will be removed.