Inheritance: One Class Built on Another
A subclass such as class Cactus(Plant) gets every attribute and method of the class it's built on, sets itself up with super().__init__(), replaces only the methods that work differently, and isinstance() tells which kind an object is.
Journeyman · Python · OOP · Updated
After this lesson you can
- Write a subclass that builds on a parent class and sets up its attributes with super().__init__()
- Override a method in a subclass, reach the parent's version with super(), and check an object's kind with isinstance()
Key takeaways
- class Cactus(Plant) makes Cactus a subclass of Plant, so every cactus has all of a plant's attributes and methods.
- super().init() runs the parent class's init, so a subclass sets up what the parent needs without copying its lines.
- A subclass overrides a method by defining one with the same name, and super() still reaches the parent's version.
- isinstance(thing, Plant) is True for a Plant and for an object of any subclass of it, such as a Cactus.
- Calling the same method on a mixed list runs each object's own version, which your textbook calls polymorphism.
Programs often keep track of several kinds of one thing: savings and checking accounts, cars and trucks, cacti and ferns. The kinds share most of what they do and differ in a few ways. Inheritance lets you write the shared part once and only the differences for each kind, and you'll meet it in libraries too, where a class of yours builds on a class the library provides.
The example is Leaf & Sprout, a houseplant app. Its Plant class knows each plant's name, how many days it goes between waterings and the date it was last watered, and its needs_water(today) method says whether enough days have passed.
Copying a class for each kind
Cacti always go 14 days between waterings. Leaf & Sprout's first try at cacti was a second class, Cactus, made by copying all of Plant and writing 14 into the copy's __init__. It worked, until a mistake was fixed in Plant.needs_water and not in the copy. From then on, plants and cacti behaved differently for no reason anyone remembered: copied code drifts apart.
A cactus is a plant, one with a fixed schedule, and Python can say exactly that.
A subclass: class Cactus(Plant)
A recipe for chocolate chip cookies can say "make the basic cookie dough, then add chocolate chips". It builds on the basic recipe instead of copying it, and a fix to the basic recipe reaches every cookie made from it. A subclass builds on another class the same way:
from datetime import date
class Plant:
def __init__(self, name, water_every, last_watered):
self.name = name
self.water_every = water_every
self.last_watered = last_watered
def needs_water(self, today):
return (today - self.last_watered).days >= self.water_every
def __str__(self):
return f"{self.name}: water every {self.water_every} days"
class Cactus(Plant):
def __init__(self, name, last_watered):
super().__init__(name, 14, last_watered)
barrel = Cactus("Barrel Cactus", date(2026, 4, 20))
print(barrel)
print(barrel.needs_water(date(2026, 5, 10)))
Barrel Cactus: water every 14 days
True
class Cactus(Plant):is read "Cactus is a kind of Plant".Plantis the parent class andCactusthe child class, or subclass. In your textbook they may be called the base class and the derived class, or the superclass and the subclass. Writing one class on top of another is inheritance.Cactusnever definesneeds_wateror__str__, yetbarrelhas both: a subclass inherits every method of its parent.super()(read "super") means "the parent class".super().__init__(name, 14, last_watered)runsPlant's__init__with 14 filled in, so a cactus gets its attributes exactly as any plant does, and making one takes only a name and a date.
Overriding a method
Ferns differ the other way: they go 3 days between waterings, and they dry out faster in summer, so from June through August they need water every 2 days. A subclass changes a method by overriding it: defining a method with the same name, which the subclass's objects use instead of the parent's.
from datetime import date
class Plant:
def __init__(self, name, water_every, last_watered):
self.name = name
self.water_every = water_every
self.last_watered = last_watered
def needs_water(self, today):
return (today - self.last_watered).days >= self.water_every
class Fern(Plant):
def __init__(self, name, last_watered):
super().__init__(name, 3, last_watered)
def needs_water(self, today):
if today.month in (6, 7, 8):
return (today - self.last_watered).days >= 2
return super().needs_water(today)
boston = Fern("Boston Fern", date(2026, 7, 8))
print("July 10:", boston.needs_water(date(2026, 7, 10)))
boston.last_watered = date(2026, 5, 8)
print("May 10:", boston.needs_water(date(2026, 5, 10)))
July 10: True
May 10: False
today.month is the date's month as a number, 7 for July. In summer, the fern's own rule answers: 2 days have passed, and that's enough. The rest of the year, super().needs_water(today) hands the question to Plant's version, so the usual rule is still written only once. Override only what's different, and let the parent do the rest.

isinstance(): which kind is it?
One list can hold plain plants, cacti and ferns together, and one loop can ask each the same question. isinstance(thing, Cactus) (read "is thing an instance of Cactus?") answers True or False, and it counts subclasses: a cactus is also a Plant.
from datetime import date
class Plant:
def __init__(self, name, water_every, last_watered):
self.name = name
self.water_every = water_every
self.last_watered = last_watered
def needs_water(self, today):
return (today - self.last_watered).days >= self.water_every
class Cactus(Plant):
def __init__(self, name, last_watered):
super().__init__(name, 14, last_watered)
plants = [Plant("Pothos", 7, date(2026, 7, 1)), Cactus("Barrel Cactus", date(2026, 7, 1))]
for plant in plants:
print(plant.name, plant.needs_water(date(2026, 7, 10)))
print([plant.name for plant in plants if isinstance(plant, Cactus)])
print(isinstance(plants[1], Plant))
Pothos True
Barrel Cactus False
['Barrel Cactus']
True
Both plants were watered on July 1, and nine days later they answer differently, because each answers with its own class's rule. The loop doesn't check which kind it has; it asks, and each object knows. Your textbook calls this polymorphism (read "pol-ee-MOR-fiz-um", from the Greek for "many shapes"): one method call, a different behavior for each kind. Use isinstance() for the times you do need the kind, such as listing only the cacti.
Do it: add herbs to the plants
Leaf & Sprout is adding kitchen herbs. An herb is a plant that goes 2 days between waterings, and its printout says it's an herb.
Plant is ready in the starter: Plant(name, water_every, last_watered), with needs_water(today) and a __str__ that returns a line such as Pothos: water every 7 days.
name: the herb's name, such as"Basil".last_watered: the date it was last watered.
Your job: write the class Herb, a subclass of Plant.
Herb(name, last_watered)makes a plant that goes2days between waterings: its__init__handsname,2andlast_wateredtoPlant's__init__withsuper().- Its
__str__returnsPlant's line with(herb)added to the end. - Everything else, such as
needs_water, comes fromPlantunchanged.
Worked through: basil = Herb("Basil", date(2026, 5, 8)) goes 2 days between waterings, so str(basil) is "Basil: water every 2 days (herb)". On May 10, 2 days have passed, so basil.needs_water(date(2026, 5, 10)) is True.
Practiced live on the lesson page: write the Python right there and check it.
Check yourself
2 questions to try on the lesson page.
So what
Inheritance keeps one copy of what every kind shares, so a fix to Plant reaches every cactus, fern and herb, and each kind writes down only what makes it different.