Inheritance


Classes can inherit functionality of other classes. If an object is created using a class that inherits from a superclass, the object will contain the methods of both the class and the superclass. The same holds true for variables of both the superclass and the class that inherits from the super class.

Python supports inheritance from multiple classes, unlike other popular programming languages.

Related Courses:

Intoduction

We define a basic class named User:

class User:
    name = ""
 
    def __init__(self, name):
        self.name = name
 
    def printName(self):
        print "Name  = " + self.name
 
brian = User("brian")
brian.printName()

This creates one instance called brian which outputs its given name. We create another class called Programmer.

class Programmer(User):
 
    def __init__(self, name):
        self.name = name
    def doPython(self):
        print "Programming Python"

This looks very much like a standard class except than User is given in the parameters. This means all functionality of the class User is accessible in the Programmer class.

Inheritance example

Full example of Python inheritance:

class User:
    name = ""
 
    def __init__(self, name):
        self.name = name
 
    def printName(self):
        print "Name  = " + self.name
 
class Programmer(User):
    def __init__(self, name):
        self.name = name
 
    def doPython(self):
        print "Programming Python"
 
brian = User("brian")
brian.printName()
 
diana = Programmer("Diana")
diana.printName()
diana.doPython()

The output:

Name  = brian
Name  = Diana
Programming Python

Brian is an instance of User and can only access the method printName. Diana is an instance of Programmer, a class with inheritance from User, and can access both the methods in Programmer and User.

Method overloading
Polymorphism
This entry was posted in Beginner and tagged , , . Bookmark the permalink.

22 Responses to Inheritance

  1. Manuel Giron says:

    Hi!, I have a question, how can I declare X number of variables in the Programmer Class, and set them when getting the instance of it. for example I want to set the programming language in the Programmer Class extending all the other parameters of the User Class.

    • Frank says:

      The traditional way is to use setter functions, in this example we have the variable name from the user class while also having language from the programmer class:

       
      class User:
          name = ""
       
          def __init__(self, name):
              self.name = name
       
          def printName(self):
              print "Name = " + self.name
       
      class Programmer(User):
       
          language = ""
       
          def setLanguage(self, language):
              self.language = language
       
          def printLanguage(self):
              print self.language
       
      manuel = Programmer("Manuel")
      manuel.setLanguage("Python")
      manuel.printName()
      manuel.printLanguage()

      A more detailed example, where variables from the User class and Programmer class are used:

      class User:
          name = None
          job = None
       
          def __init__(self, name, job):
              self.name = name
              self.job = job
       
          def printName(self):
              print "Name = " + self.name
       
          def printJob(self):
              print "Job = " + self.job
       
      class Programmer(User):
       
          language = ""
       
          def setLanguage(self, language):
              self.language = language
       
          def printLanguage(self):
              print "Language = " + self.language
       
      guido = Programmer("Guido","Developer")
      guido.setLanguage("Python")
      guido.printName()
      guido.printLanguage()
      guido.printJob()

      An instance can be created only from one class, thus either the Programmer class or User class. If we overwrite the User constructor, we can no longer access its variables, thus we have to pick one constructor. If we pick the Programmers constructor we would have:

      class User:
          name = None
          job = None
       
          def setName(self,name):
              self.name = name
       
          def setJob(self,job):
              self.job = job
       
          def printName(self):
              print "Name = " + self.name
       
          def printJob(self):
              print "Job = " + self.job
       
      class Programmer(User):
       
          language = ""
       
          def __init__(self, language):
              self.language = language
       
          def printLanguage(self):
              print "Language = " + self.language
       
      guido = Programmer("Python")
      guido.setName("Guido")
      guido.setJob("Developer")
      guido.printName()
      guido.printLanguage()
      guido.printJob()

      To set multiple variables you can either pass a list/tuple to an setter function or define various setter functions and call them one by one. I hope that helps, let me know if you have any questions