Python Inheritance

JK JK 2018 
Created at
Updated at  
7,186 0 0

Inheritance allows us to define a class that inherits all the methods and properties from another class.

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 Class

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

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 class that inherits the functionality from another class, send the parent class as a parameter when creating the child class:

class Student(Person):
  pass

Now the Student class has the same properties and methods as the Person class.

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

Add the __init__() Function

So far we have created a child class that inherits the properties and methods from its parent.

We want to add the __init__() function to the child class (instead of the pass keyword).

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

When you add the __init__() function, the child class will no longer inherit the parent's __init__() function.

To keep the inheritance of the parent's __init__() function, add a call to the parent's __init__() function:

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

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 __init__() function.


Use the super() Function

Python also has a super() function that will make the child class inherit all the methods and properties from its parent:

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

By using the super() function, you do not have to use the name of the parent element, it will automatically inherit the methods and properties from its parent.


Add Properties

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

In the example below, the year 2019 should be a variable, and passed into the Student class when creating student objects. To do so, add another parameter in the __init__() function:

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

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

Add Methods

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 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.

Tags Child Class Parent Class Python Python Class Python Inheritance Facebook X
Comments 0
Similar posts
  1. Python Iterators
    7,104
  2. Python Classes/Objects
    7,466
  3. Python Polymorphism
    7,086
  4. Python Scope
    7,062
  5. Python Arrays
    7,086
  6. Python Modules
    7,097
  7. Python Lambda
    7,101
  8. Python Functions
    7,062
  9. Python While Loops/For Loops
    7,504
  10. Python Conditions and If statements
    7,128
  11. Python Dictionaries
    7,053
  12. Python Sets
    7,090
  13. Python Tuples
    7,165
  14. Python Comparison Operators
    7,237
  15. Python Arithmetic Operators
    7,075
  16. Printing string n times
    7,065
  17. String concatenation by join()
    7,130
  18. Python Lists
    7,335
  19. Python String Operations
    7,406
  20. Python Data Types
    7,433
  21. Python Variables
    7,395
  22. Python Comments
    7,533
  23. Python Syntax
    7,405
  24. Python Getting Started
    7,453
  25. Python Introduction
    7,100
  26. What is Python?
    7,723
  27. Code Chronicles - A Caffeine-Fueled Journey into Data Software Engineering
    7,361
  28. Machine Learning Types and Programming Languages
    7,230
  29. Python Tutorials for AP Computer Science Principles, Data Projects and High School Internship
    7,888
  30. Mastering Excel Data Manipulation with Python
    7,067
  31. Challenge: One Code Problem Per Day
    1,093
  32. Clean Python Environments: The Power of venv vs. Docker
    826
  1. Clean Python Environments: The Power of venv vs. Docker
    826
  2. How to Build Llama 3 AI Apps with Python: Setup & User Prompts
    810
  3. Mastering Excel Data Manipulation with Python
    7,067
  4. Try...Catch Helps Ignoring Data Type Miss-Match Error in Python
    9,196
  5. RegExp example in Python to exclude javascript from HTML code
    7,053
  6. Python code to convert from Lunar to Solar
    7,723
  7. Python example to download webpage
    8,591
  8. Python Tutorials for AP Computer Science Principles, Data Projects and High School Internship
    7,888
  9. Python Modules
    7,097
  10. Python Scope
    7,062
  11. Python Polymorphism
    7,086
  12. Python Iterators
    7,104
  13. Python Classes/Objects
    7,466
  14. Python Arrays
    7,086
  15. Python Lambda
    7,101
  16. Python Functions
    7,062
  17. Python While Loops/For Loops
    7,504
  18. Python Conditions and If statements
    7,128
  19. Python Dictionaries
    7,053
  20. Python Sets
    7,090
  21. Python Tuples
    7,165
  22. Python Comparison Operators
    7,237
  23. Python Arithmetic Operators
    7,075
  24. Printing string n times
    7,065
  25. String concatenation by join()
    7,130
  26. Python Lists
    7,335
  27. Python String Operations
    7,406
Recently updated
  1. Watch This Dazzling Dancer’s Mind-Blowing Moves
    50
  2. Michael Jackson's Billie Jean
    131
  3. Bootstrap vs. Tailwind CSS: Origins, Features, Pros & Cons, and How to Choose the Right Framework
    139
  4. The Complete Guide to Golang: History, Features, Real-World Uses, and Code Examples
    418
  5. Telemetry vs. Analytics: Understanding the Difference and Why It Matters
    236
  6. The Evolution and Production Reality of Agentic AI
    214
  7. How to Activate or Waive Your UIUC Student Health Insurance
    297
  8. Complete Guide to Building a Machine Learning Model
    325
  9. My life cuts at Las Vegas during Thanksgiving day holiday
    7,407
  10. The Cybercab Transformation: From Autonomous Taxi to Mobile Base Station
    354
  11. Harness vs. OpenClaw: Two Very Different "Agents"
    980
  12. Clean Python Environments: The Power of venv vs. Docker
    826
  13. What is Docker? Why is Docker also useful in a development environment?
    621
  14. UIUC 2026-2027 Academic Calendar
    1,597
  15. How to Build Llama 3 AI Apps with Python: Setup & User Prompts
    810
  16. Open-Source LLMs: The AI Revolution
    762
  17. Resume 2.0: Leveling Up for My First Software Gig
    2,229
  18. Not everyone will understand what this man just did
    1,888
  19. UIUC Dorm Guide: Find Your Perfect Fit !!
    1,596
  20. Unpacking IU's Shopper
    769
  21. Jackie Chan's Police Story: The Action Masterpiece
    664
  22. The IVE Story: Identity, 'I AM' Charts, and Influence
    968
  23. Tech Visionaries who graduated at UIUC - You are the Next Turn
    1,187
  24. Open Databases for Sex Crime Occurrences in the U.S.
    718
  25. Automatically copy text to the clipboard when dragging the mouse in the Cursor
    2,653
  26. My First Day at University of Illinois-Urvana Champaign
    1,197
  27. Sand, Sea, and a Splash of Fun at Newport Beach: A Family Adventure
    8,134
  28. Sun, Rocks, and Adventure: A Day at Joshua Tree National Park
    8,216
  29. Sipping the Stars: My Starbucks Adventure
    9,659
  30. Exciting explore at Sequoia National Park
    7,664
  31. My Life Shot at Death Valley
    1,713
  32. Ip Man fights with Muay Thai Master
    944
  33. Mad Clown - Don't Die
    1,027
  34. How to get Student Enrollment and Degree Verification at UIUC
    4,906
  35. LAX Thanksgiving Rush: A Joyful Reunion
    925
  36. ZO ZAZZ(조째즈) - Don`t you know (모르시나요) (PROD.ROCOBERRY)
    1,131
  37. FISHINGIRLS Unleashes Energetic EP 'Funiverse' Featuring Signature Track 'Fishing King'
    987
  38. 10CM - To Reach You (너에게 닿기를)
    1,165
  39. Feeling weak? Transform yourself at the UIUC ARC!
    1,573
  40. BOYNEXTDOOR - If I Say I Love You
    1,188
  41. The Future of Software Engineer - AI Engineering
    932
  42. G Dragon x Taeyang (Eyes Nose Lips, Power, Home Sweet Home, GOOD BOY) - LE GALA PIÈCES JAUNES 2025
    920
  43. Lie - Legend song by BIGBANG
    7,820
  44. Why ROLLBACK is useful when you work with Google Gemini CLI?
    844
  45. Reimbursement after Vaccination at McKinley Health Center
    1,023
  46. Gemini CLI makes a Magic! Time to speed up your app development with Google Gemini CLI!
    968
  47. Common Questions from UIUC school life in terms of CS Program
    1,125
  48. UIUC Immunization Compliance
    1,213
  49. LEE CHANHYUK's songs really resonate with my soul - Time Stop! Vivid LaLa Love, Eve, Endangered Love ...
    1,099
  50. LEE CHANHYUK - Endangered Love (멸종위기사랑)
    1,096