Tag: inhertiance
python class 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 Course:
Python Programming Bootcamp: Go from zero to hero
Intoduction
We define a basic class named User:
class User: |
This creates one instance called brian which outputs its given name. We create another class called Programmer.
class Programmer(User): |
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: |
The output:
Name = brian |
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.
If you are new to Python programming, I highly recommend this book.